P9 - Pengenalan PHP - Pemrograman Web
P9 - Pengenalan PHP - Pemrograman Web
. Buat direktori 'php' didalam direktori c:/xampp/htdocs/ c:/xampp/htdocs/ adalah direktori server php dengan menggunakan xampp 2. Simpan file percobaan di direktori c:/xampp/htdocs/php/ 3. Buka hasil percobaan melalui browser http://localhost/php/namafile.php Percobaan: 1. tes.php: Sintaks PHP dasar: <!DOCTYPE html> <html> <body> <h1>My first PHP page</h1> <?php echo "Learning PHP programming!"; ?> </body> </html> 2. komen.php: Komentar dalam PHP <!DOCTYPE html> <html> <body> <?php //This is a PHP comment line /* This is a PHP comment block */ ?> </body> </html> 3. variabel.php: Variabel PHP <!DOCTYPE html> <html> <body> <?php $x=15; $y=63; $z=$x+$y; echo $z; ?> </body> </html>
Politeknik Elektronika Negeri Surabaya (PENS) ---------------------------------------------------------4. variabel1.php: Variable skup lokal <?php $x=50; // global scope function myTest() { echo $x; // local scope } myTest(); ?> 5. variabel2.php: Variabel skup global <!DOCTYPE html> <html> <body> <?php $x=25; // global scope $y=50; // global scope function myTest() { global $x,$y; $y=$x+$y; } myTest(); // run function echo $y; // output the new value for variable $y ?> </body> </html> 6. variabelglob.php: Penggunaan Variabel GLOBAL php <?php $x=50; $y=60; function myTest() { $GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y']; } myTest(); echo $y; ?> 7. variabel3.php: Variabel static <!DOCTYPE html> <html>
Politeknik Elektronika Negeri Surabaya (PENS) ---------------------------------------------------------<body> <?php function myTest() { static $x=10; echo $x; $x++; } myTest(); myTest(); myTest(); ?> </body> </html> 8. variabel4.php: Variabel skup parameter <!DOCTYPE html> <html> <body> <?php function myTest($x) { echo $x; } myTest(50); ?> </body> </html> 9. varstring.php: Variabel string <!DOCTYPE html> <html> <body> <?php $txt="PENS JOSS!"; echo $txt; ?>
Politeknik Elektronika Negeri Surabaya (PENS) ---------------------------------------------------------</body> </html> 10. concat.php: Penggabungan dua string <!DOCTYPE html> <html> <body> <?php $txt1="Teknik Informatika"; $txt2="Politeknik Elektronika Negeri Surabaya"; echo $txt1 . " " . $txt2; ?> </body> </html> 11. operatorarit.php: Operator Aritmatika <html> <head><title>Arithmetical Operators</title><head> <body> <?php $a = 42; $b = 20; $c = $a + $b; echo "Addtion Operation Result: $c <br/>"; $c = $a - $b; echo "Substraction Operation Result: $c <br/>"; $c = $a * $b; echo "Multiplication Operation Result: $c <br/>"; $c = $a / $b; echo "Division Operation Result: $c <br/>"; $c = $a % $b; echo "Modulus Operation Result: $c <br/>"; $c = $a++; echo "Increment Operation Result: $c <br/>"; $c = $a--; echo "Decrement Operation Result: $c <br/>"; ?> </body> </html>
Politeknik Elektronika Negeri Surabaya (PENS) ---------------------------------------------------------<?php $a = 42; $b = 20; if( $a == $b ){ echo "TEST1 : a is equal to b<br/>"; }else{ echo "TEST1 : a is not equal to b<br/>"; } if( $a > $b ){ echo "TEST2 : a is greater than b<br/>"; }else{ echo "TEST2 : a is not greater than b<br/>"; } if( $a < $b ){ echo "TEST3 : a is less than b<br/>"; }else{ echo "TEST3 : a is not less than b<br/>"; } if( $a != $b ){ echo "TEST4 : a is not equal to b<br/>"; }else{ echo "TEST4 : a is equal to b<br/>"; } if( $a >= $b ){ echo "TEST5 : a is either grater than or equal to b<br/>"; }else{ echo "TEST5 : a is nieghter greater than nor equal to b<br/>"; } if( $a <= $b ){ echo "TEST6 : a is either less than or equal to b<br/>"; }else{ echo "TEST6 : a is nieghter less than nor equal to b<br/>"; } ?> </body> </html> 13. operatorlog.php: Operator logical <html> <head><title>Logical Operators</title><head> <body> <?php $a = 42; $b = 0; if( $a && $b ){ echo "TEST1 : Both a and b are true<br/>";
Politeknik Elektronika Negeri Surabaya (PENS) ---------------------------------------------------------}else{ echo "TEST1 : Either a or b is false<br/>"; } if( $a and $b ){ echo "TEST2 : Both a and b are true<br/>"; }else{ echo "TEST2 : Either a or b is false<br/>"; } if( $a || $b ){ echo "TEST3 : Either a or b is true<br/>"; }else{ echo "TEST3 : Both a and b are false<br/>"; } if( $a or $b ){ echo "TEST4 : Either a or b is true<br/>"; }else{ echo "TEST4 : Both a and b are false<br/>"; } $a = 10; $b = 20; if( $a ){ echo "TEST5 : a is true <br/>"; }else{ echo "TEST5 : a is false<br/>"; } if( $b ){ echo "TEST6 : b is true <br/>"; }else{ echo "TEST6 : b is false<br/>"; } if( !$a ){ echo "TEST7 : a is true <br/>"; }else{ echo "TEST7 : a is false<br/>"; } if( !$b ){ echo "TEST8 : b is true <br/>"; }else{ echo "TEST8 : b is false<br/>"; } ?> </body> </html>
Politeknik Elektronika Negeri Surabaya (PENS) ---------------------------------------------------------<?php $a = 42; $b = 20; $c = $a + $b; /* Assignment operator */ echo "Addtion Operation Result: $c <br/>"; $c += $a; /* c value was 42 + 20 = 62 */ echo "Add AND Assigment Operation Result: $c <br/>"; $c -= $a; /* c value was 42 + 20 + 42 = 104 */ echo "Subtract AND Assignment Operation Result: $c <br/>"; $c *= $a; /* c value was 104 - 42 = 62 */ echo "Multiply AND Assignment Operation Result: $c <br/>"; $c /= $a; /* c value was 62 * 42 = 2604 */ echo "Division AND Assignment Operation Result: $c <br/>"; $c %= $a; /* c value was 2604/42 = 62*/ echo "Modulus AND Assignment Operation Result: $c <br/>"; ?> </body> </html> 15. operatorcond.php: Operator kondisi <html> <head><title>Arithmetical Operators</title><head> <body> <?php $a = 10; $b = 20; /* If condition is true then assign a to result otheriwse b */ $result = ($a > $b ) ? $a :$b; echo "TEST1 : Value of result is $result<br/>"; /* If condition is true then assign a to result otheriwse b */ $result = ($a < $b ) ? $a :$b; echo "TEST2 : Value of result is $result<br/>"; ?> </body> </html> 16. ifelse.php: if..else statement <html> <body> <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; elseif ($d=="Sun") echo "Have a nice Sunday!"; else
Politeknik Elektronika Negeri Surabaya (PENS) ---------------------------------------------------------echo "Have a nice day!"; ?> </body> </html> 17. switch.php: switch statement <html> <body> <?php $d=date("D"); switch ($d) { case "Mon": echo "Today is Monday"; break; case "Tue": echo "Today is Tuesday"; break; case "Wed": echo "Today is Wednesday"; break; case "Thu": echo "Today is Thursday"; break; case "Fri": echo "Today is Friday"; break; case "Sat": echo "Today is Saturday"; break; case "Sun": echo "Today is Sunday"; break; default: echo "Wonder which day is this ?"; } ?> </body> </html> 18. forloop.php: for loop statement <html> <body> <?php $a = 0; $b = 0; for( $i=0; $i<5; $i++ )
Politeknik Elektronika Negeri Surabaya (PENS) ---------------------------------------------------------{ $a += 10; $b += 5; } echo ("At the end of the loop a=$a and b=$b" ); ?> </body> </html> 19. whileloop.php: While loop statement <html> <body> <?php $i = 0; $num = 50; while( $i < 10) { $num--; $i++; } echo ("Loop stopped at i = $i and num = $num" ); ?> </body> </html> 20. dowhile.php: do..while loop statement <html> <body> <?php $i = 0; $num = 0; do { $i++; }while( $i < 10 ); echo ("Loop stopped at i = $i" ); ?> </body> </html> 21. Foreach.php: for each statement <html> <body> <?php $array = array( 1, 2, 3, 4, 5); foreach( $array as $value )
Politeknik Elektronika Negeri Surabaya (PENS) ---------------------------------------------------------{ echo "Value is $value <br />"; } ?> </body> </html> 22. Array1.php: PHP array <html> <body> <?php /* First method to create array. */ $numbers = array( 1, 2, 3, 4, 5); foreach( $numbers as $value ) { echo "Value is $value <br />"; } /* Second method to create array. */ $numbers[0] = "one"; $numbers[1] = "two"; $numbers[2] = "three"; $numbers[3] = "four"; $numbers[4] = "five"; foreach( $numbers as $value ) { echo "Value is $value <br />"; } ?> </body> </html> 23. Array2.php: PHP array <html> <body> <?php /* First method to associate create array. */ $salaries = array( "mohammad" => 2000, "qadir" => 1000, "zara" => 500 ); echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />"; echo "Salary of qadir is ". $salaries['qadir']. "<br />"; echo "Salary of zara is ". $salaries['zara']. "<br />"; /* Second method to create array. */
Politeknik Elektronika Negeri Surabaya (PENS) ---------------------------------------------------------$salaries['mohammad'] = "high"; $salaries['qadir'] = "medium"; $salaries['zara'] = "low"; echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />"; echo "Salary of qadir is ". $salaries['qadir']. "<br />"; echo "Salary of zara is ". $salaries['zara']. "<br />"; ?> </body> </html> 24. Arraymulti.php: Multi Dimensional array. <html> <body> <?php $marks = array( "mohammad" => array ( "physics" => 35, "maths" => 30, "chemistry" => 39 ), "qadir" => array ( "physics" => 30, "maths" => 32, "chemistry" => 29 ), "zara" => array ( "physics" => 31, "maths" => 22, "chemistry" => 39 ) ); /* Accessing multi-dimensional array values */ echo "Marks for mohammad in physics : " ; echo $marks['mohammad']['physics'] . "<br />"; echo "Marks for qadir in maths : "; echo $marks['qadir']['maths'] . "<br />"; echo "Marks for zara in chemistry : " ; echo $marks['zara']['chemistry'] . "<br />"; ?> </body> </html>