PHP Programming

Elfan Nofiari

IF-ITB/EN/Mar-05 IF3292 – PHP Programming

Page 1

postgreSQL. embedded script yang diinterpretasi (bukan di-compile) • PHP singkatan dari: – Personal Home Page – Professional Home Page – PHP: Hypertext Preprocessor • • • • • Official Website : http://www. IMAP.3.3 Free & opensource Multi platform: Windows. JPEG. SWF dll IF-ITB/EN/Mar-05 IF3292 – PHP Programming Page 2 . GIF. POP3. Oracle. IBM DB2. ODBC. XML.0. dll Output : HTML. PNG.Sekilas tentang PHP • Bahasa pemrograman script di sisi server • Berupa script yang disisipkan di dalam dokumen HTML. SNMP. dll Protocol : HTTP.10 & 5. PDF. LDAP. FTP. Linux. Mac Menyediakan Library/API yang menyeluruh: – – – – Database : MySQL.net PHP versi terakhir : 4.php.

Contoh Penyisipan Script PHP PHP <html> <? /* Cara I: script PHP dideklarasikan di sini*/ echo "halo 1<br>". ?> <script language="php"> /* Cara III: script PHP dideklarasikan di sini*/ echo "halo 3". </script> </html> HTML <html> <html> halo 1<br> halo 1<br> halo 2<br> halo 2<br> halo 3</html> halo 3</html> Browser IF-ITB/EN/Mar-05 IF3292 – PHP Programming Page 3 . ?> <?php /* Cara II: script PHP dideklarasikan di sini*/ echo "halo 2<br>".

Konstanta terdefinisi: PHP_VERSION. // $foo is integer (15) • pendefinisian tipe: • variable scope: – – – – – local (default) global: global $var.14). dengan tipe data: – – – – – – – Jenis scalar : integer. static: static $var. (float)$var. $GLOBALS["var"]. Pendefinisian: define("pi". string. associative).Identifier. PHP_OS. strval($var) type casting: (integer)$var.3) $foo = 5 + "10 Little Piggies". object Jenis khusus : resource. float/double. // $foo is now an integer (2) $foo = $foo + 1. NULL fungsi settype: settype($var."integer"). floatval($var). boolean Jenis compound : array (indexed. Tipe. // $foo is now a float (3. __FILE__. 3. // $foo is string (ASCII 48) $foo += 2. __LINE__.3. (string)$var type juggling: $foo = "0". dll • konstanta IF-ITB/EN/Mar-05 IF3292 – PHP Programming Page 4 . Konstanta • • • • case sensitive variable dengan prefiks: $ variable tidak perlu dideklarasi weakly typed. Variabel. fungsi konversi: intval($var).

<= Operator logika: && (and). *=.Operator • • • • • • • Assignment: =. >=. ~ (not). *. +=. >. %= Aritmatika: +. • Operator eksekusi: $output = `ls -l`. -. --$a. • Operator new (object): new Page 5 IF-ITB/EN/Mar-05 IF3292 – PHP Programming . >> (shift right) Operator kondisi: ? $jenis = ($bil%2==0) ? ”Genap” : ”Ganjil”. | (or). === (identical). $b-Perbandingan: == (equal). • Operator string: . ^ (xor).txt") or die("Can't open file"). /. ! (not) Bitwise: & (and). (concat) • Error Control: $my_file = @file("data. !=. /=. -=. % Operator pre/post increment/decrement: ++$a. $b++. || (or). << (shift left). <.

. case.Konstruksi dasar program PHP • Sequence • Pemilihan – if – if .. break – – – – while do . while for foreach • Pengulangan • foreach (array_expression as $value) <statement> • foreach (array_expression as $key => $value) <statement> • Pencabangan – break – continue IF-ITB/EN/Mar-05 IF3292 – PHP Programming Page 6 . elseif – switch .. else.

} $printFunction = "printItalic". } function printBold($txt) { echo "<B>".3). echo $newfunc(2. "</B> <BR>".($a + $b)."\n".Fungsi • • • Passing parameter by value: function increment($number) {…}.'return "$a + $b = ".'). $printFunction("Italic").$b'. //<I>Italic</I> <BR> $printFunction = "printBold". //<B>Bold</B> <BR> ?> • Dynamic Function Creation: <? $newfunc = create_function('$a. Dynamic Function Call: <? function printItalic($txt) { echo "<I>". //2 + 3 = 5 ?> IF-ITB/EN/Mar-05 IF3292 – PHP Programming Page 7 . $printFunction("Bold"). "</I> <BR>". $txt . Passing parameter by reference: function increment(&$number) {…}. $txt .