Pengantar Teknologi MobilePHP

Download Report

Transcript Pengantar Teknologi MobilePHP

Pengantar Teknologi Mobile PHP
Antonius Rachmat C, S.Kom
PHP: Hypertext Preprocessors
PHP
• PHP (PHP: Hypertext Preprocessor)
dikembangkan oleh Rasmus Lerdorf (1994),
sebagai awal pengembangan untuk HTTP
usage logging dan server-side form generation
pada Unix.
• PHP 2 (1995) diubah menjadi bahasa Serverside embedded scripting. Ditambahkan
kemampuan database support, file uploads,
variabel, array, rekursif, kondisi, iteration, regular
expressions, dll.
Sejarah PHP
• PHP 3 (1998) ditambah dukungan untuk ODBC,
multiple platform support, protokol email
(SNMP,IMAP), dan parser baru oleh Zeev
Suraski and Andi Gutmans .
• PHP 4 (2000) menjadi komponen independen
dari web server agar lebih efisien. Parser diganti
nama menjadi Zend Engine.
• PHP 5 (2004) ditambahkan Zend Engine II
dengan OOP, dukungan XML dengan pustaka
libxml2, SOAP untuk Web Services, SQLite telah
ditambahkan dalam PHP
Sejarah PHP
• Pada August 2004, PHP digunakan pada 16,946,328
domains, 1,348,793 alamat IP
(http://www.php.net/usage.php) Hampir 32% dari seluruh
domain web.
Mengapa PHP digunakan?
• Mudah Pemakaiannya
– Kode ditanamkan pada HTML/WML/XHTML.
– Kode php dimulai dan diakhiri dengan tanda
(tag)
<html>
<head>
<title>Example</title>
</head>
<body>
<?php
echo "Hi, I'm a PHP script!";
?>
</body>
</html>
Why PHP?
• Cross Platform
– Dapat jalan hampir di semua web server pada beberapa sistem
operasi
– Web server: Apache, Microsoft IIS, Caudium, Netscape
Enterprise Server
– Sistem operasi: NIX (HP-UX,OpenBSD,Solaris,Linux), Mac
OSX, Windows NT/98/2000/XP/2003
– Database: Adabas D, dBase,Empress, FilePro (read-only),
Hyperwave,IBM DB2, Informix, Ingres, InterBase, FrontBase,
mSQL, Direct MS-SQL, MySQL, ODBC, Oracle (OCI7 and
OCI8), Ovrimos, PostgreSQL, SQLite, Solid, Sybase,
Velocis,Unix dbm
• Keuntungan dalam biaya
– PHP is free.
Dukungan PHP
• GD (GIF, JPEG,
PNG)
• SNMP
• IMAP (POP, NNTP)
• FTP
• XML parser
• PDF generation
• DCOM (Win32 only)
• SWF (Flash)
•zlib (compressed IO)Charset/text
conversion (UTF-8, Cyrillic,
Hebrew)
•CCVS (Credit Card Verification
System)
•SOAP
•Cybercash
•ASPELL/PSPELL
PHP Model
• Source:
<html>
<?php
echo(“Hello World!”);
?>
</html>
• Menjadi:
<html>
Hello World!
</html>
Notasi PHP
Boolean
• Untuk literal boolean: true atau false
• Berikut dianggap bernilai false, jika
– Nilai boolean FALSE
– Nilai integer 0 atau float 0.0
– String yang kosong, atau string “0”
– Array dengan elemen kosong
– NULL (termasuk unset variabel)
Contoh Integer
• Nilai literal integer yang valid:
–
–
–
–
$a = 1234;
$a = -123;
$a = 0123; #octal number
$a = 0x123; #hexa number
• Jika nilai literal integer melebihi range Integer,
otomatis PHP akan mengkonversi ke tipe float
• Konversi ke Integer:
– Nilai boolean FALSE => 0, TRUE => 1
– Casting (int). Contoh:
• $a = (int) (25/7); # int(3)
• $a = round(25/7); # float(4)
Float
• Ukuran float tergantung pada platform,
walaupun maksimum ~1.8e208 (64 bit
format IEEE)
– $a = 1.234;
– $b = 1.2e4;
– $c = 7E-10;
String
• Literal string dideklarasikan baik dengan
– Petik ganda (“ “). contoh: $a = “Nama: $nama\n”;
– Petik tunggal (' '). contoh: $b = 'c:\*.*';
– Heredoc. Contoh:
$c = <<<EOD
Ini testing
EOD;
• Pengaksesan karakter string
–
–
–
–
$a = “ini test”;
$pertama = $a{0};
$ketiga = $a{2};
$akhir = $a{strlen($a)-1};
Konversi String ke Angka
• Contoh:
– $a = 1 + “10.5”;
– $a = 1 + “-1.3e3”;
– $a = 1 + “bob-1.3e3”;
– $a = 1 + “bob3”;
– $a = 1 + “10 ayam kate”;
– $a = 1 + “10.2 ayam kate”;
– $a = “10.0 ayam” + 1;
array
• Simple array:
$a = array(1, 2, 3, 4, 5);
print_r($a);
foreach($a as $i => $value) { unset($a[$i]); }
print_r($a);
$a[] = 6;
print_r($a);
$a = array_values($a);
$a[] = 7;
print_r($a);
Custom Array
• Contoh pembuatan array dengan custom
key
<?php
// This array is the same as ...
array(5 => 43, 32, 56, "b" => 12);
// ...this array
array(5 => 43, 6 => 32, 7 => 56, "b" => 12);
?>
Array Multidimensi
NULL
• NULL menyatakan variabel yang tidak ada
nilainya
• Sebuah variabel NULL, jika
– Dinyatakan sebagai NULL dengan opertor =
– Belum pernah diberikan suatu nilai literal
– Telah di unset()
• Untuk mengecek apakah variabel NULL
atau tidak, dapat digunakan fungsi
is_null()
Konvensi PHP
• Aturan nama variabel PHP mengikuti konvensi seperti
bahasa pemrograman lainnya
• Untuk membuat referensi ke variabel lain
$a = “test”;
$b = &$a; # referensi
$b = “ayam”;
echo ($a . ' == ' . $b);
• Variabel dari variabel
$a = “hello”;
$$a = “world”;
echo “$a ${$a}”;
echo “$a $hello”;
Ruang Lingkup
• Contoh berikut, variabel $a dapat diakses dari dalam file include
atau require
<?php
$a = 1;
include "b.inc";
?>
• Variabel bersifat lokal dalam suatu fungsi
<?php
$a = 1; /* global scope */
function Test() {
echo $a; /* menunjuk ke lokal variabel */
}
Test();
?>
Operator
• Aritmatika
• Assignment
Operator Perbandingan
Ternary operator
<?php
// Contoh Ternary Operator
$action = (empty($_POST['action'])) ? 'default' :
$_POST['action'];
// sama dengan if/else berikut:
if (empty($_POST['action'])) {
$action = 'default';
} else {
$action = $_POST['action'];
}
?>
Perbandingan
<?php
if ($a > $b) {
echo "a is bigger than b";
} elseif ($a == $b) {
echo "a is equal to b";
} else {
echo "a is smaller than b";
}
?>
Struktur While
<?php
$i = 1;
while ($i <= 10) {
echo $i++;
}
$i = 1;
while ($i <= 10):
echo $i;
$i++;
endwhile;
?>
<?php
$i = 10;
do{
echo $i;
}while($i>=1);
?>
Struktur for
Foreach (1)
<?php
$arr = array("one", "two", "three");
reset($arr);
while (list($key, $value) = each ($arr)) {
echo "Key: $key; Value: $value<br />\n";
}
foreach ($arr as $key => $value) {
echo "Key: $key; Value: $value<br />\n";
}
?>
Foreach (2)
<?php
$arr = array("one", "two", "three");
reset ($arr);
while (list(, $value) = each ($arr)) {
echo "Value: $value<br />\n";
}
foreach ($arr as $value) {
echo "Value: $value<br />\n";
}
?>
Switch
Switch (2)
Kilas balik WML
• Wireless Markup Language (WML)
– Dokumen berbasis XML untuk
dipresentasikan pada micro browser
• WML dipertukarkan melalui protokol
Wireless Application Protocol (WAP)
• WML merupakan aplikasi XML, sehingga
aturan nya mengikuti XML
• Ukuran maksimum WML deck 1492 byte.
Komunikasi
• Komunikasi antara web server dengan
perangkat nirkabel WAP-enabled,
membutuhkan WAP Gateway
Wmlheader.inc
MIME apache
• MIME: Multipurpose Internet Mail
Extensions
• Agar Apache dapat menterjemahkan tipe
file yang diminta oleh user, Apache harus
tahu tipe MIMEnya
• Untuk WML
– AddType application/x-httpd-php .wml
– AddType application/x-httpd-php3 .wml
– AddType text/vnd.wap.wml .wml
– AddType image/vnd.wap.wbmp .wbmp
WML
Wap2.php
Mengirim email
• Deck 1 (email1.php):
– Kirim header WML.
– Masukkan penerima Email.
– Masukkan subjek email.
– Masukkan pesan email.
– Kirim informasi ke deck kedua (email2.php)
• Deck 2 (email2.php):
– Kirim email ke penerima.
– Konfirmasi ke user bahwa email telah terkirim
Email1.php
Email2.php
Berhasil!