Dasar PHP Aplikasi IT2 Donny Reza Syntax PHP














- Slides: 14
Dasar PHP Aplikasi IT-2 Donny Reza
Syntax PHP <? php echo ‘Diparsing oleh PHP’; ? > <p>Paragraph di dalam HTML, akan diabaikan oleh PHP</p> Tulisan ini juga akan diabaikan oleh PHP <? php echo ‘Ini juga Diparsing oleh PHP’; ? > PHP juga dapat disisipkan di <? php echo ‘tengah-tengah’; ? >
Komentar <? php // Komentar untuk satu barus /* komentar untuk lebih dari satu baris */ # komentar untuk satu baris juga ? >
Tipe-tipe Variabel <? php //boolean $benar = true; $salah = false; //integer $a = 120; //decimal $a = -120; //negatif $a = 0123; //octal = basis 8, setara dengan 83 desimal $a = 0 x 1 A; //hexadecimal, basis 16, setara dengan 26 desimal ? >
Tipe-tipe Variabel <? php //floating point $b = 1. 24; //string $c 1 = ‘String sederhana’; //single quote $c 2 = “String sederhana”; //double quotes $c 3 = “Sisipkan variabel $a di tengah”; $c 4 = “Sisipkan variabel {$a} di tengah”; $c 5 = “Sisipkan “. $a. ” di tengah-tengah”; $c 6 = “Sisipkan ‘. $a. ’ di tengah-tengah”; ? >
Tipe-tipe Variabel <? php //array $b = array( “A”, “B”, “C” ); //deklarasi array $b[3] = “D”; $b[‘huruf’] = “E”; //melihat isi array echo $b[0]; echo $b[‘huruf’]; $b = array( 1=>”A”, 2=>” 3” ); //deklarasi array ? >
Tipe-tipe Variabel <? php // Array Ini: $a = array( 'color' => 'red', 'taste' => 'sweet', 'shape' => 'round', 'name' => 'apple', 4 // key (index) akan bernilai 0 ); $b = array('a', 'b', 'c'); //. . . ekuivalen dengan: $a = array(); $a['color'] = 'red'; $a['taste'] = 'sweet'; $a['shape'] = 'round'; $a['name'] = 'apple'; $a[] = 4; // key will be 0 $b = array(); $b[] = 'a'; $b[] = 'b'; $b[] = 'c'; ? >
Konstanta <? php // konstanta yang valid define("FOO", "something"); define("FOO 2", "something else"); define("FOO_BAR", "something more"); // konstanta invalid define("2 FOO", "something"); //valid tapi sebaiknya dihindari define("__FOO__", "something"); //menampilkan/memanggil nilai konstanta echo FOO; ? >
Expression <? php $b = $a = 5; /* assign the value five into the variable $a and $b */ $c = $a++; /* post-increment, assign original value of $a (5) to $c */ $e = $d = ++$b; /* pre-increment, assign the incremented value of $b (6) to $d and $e */ ? >
Control Structures IF //satu baris statement, tanda kurung { } tidak harus ada <? php if ($a > $b) echo "a is bigger than b"; ? > //lebih dari satu baris statement, tanda kurung {} wajib ada <? php if ($a > $b) { echo "a is bigger than b"; $b = $a; } ? >
Control Structures Else <? php if ($a > $b) { echo "a is greater than b"; } else { echo "a is NOT greater than b"; } ? >
Control Structures Elseif/Else If <? 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"; } ? >
Control Structures Alternative Syntax (IF, Else IF ) <? php if ($a == 5): echo "a equals 5"; echo ". . . "; elseif ($a == 6): echo "a equals 6"; echo "!!!"; else: echo "a is neither 5 nor 6"; endif; ? >
Function <? php function foo() { echo "Example function. n"; } ? > Function dengan argumen <? php function foo( $input ) { echo “Test “. $input; } ? >