PHP Lanjutan
Euis Marlina, S.Kom
Email : [email protected]://euismarlina.edublogs.orgHP : 08179424319
Kondisi
If
syntax :
if (expr)
statement;
<?phpif ($a > $b) echo "a is bigger than b";?>
Else
contoh :
<?phpif ($a > $b) { echo "a is bigger than b";}
else
{ echo "a is NOT bigger than b";}?>
Else if
Contoh :<?phpif ($a > $b) { echo "a is bigger than b";} else if ($a == $b) { echo "a is equal to b";} else { echo "a is smaller than b";}?>
Switch
contoh :<?php
switch ($i) {case 0: echo "i equals 0"; break;case 1: echo "i equals 1"; break;case 2: echo "i equals 2"; break;}?>
Switch untuk string
<?phpswitch ($i) {case "apple": echo "i is apple"; break;case "bar": echo "i is bar"; break;case "cake": echo "i is cake"; break;}?>
Perulangan
Whilesyntax :while (expr)
statement;
Contoh:<?$i = 1;while ($i <= 10) { echo $i++; echo "<br />"; }?>
do-while
syntax :
<?php$i = 0;do { echo $i;} while ($i > 0);?>
Forsyntax :for (expr1; expr2; expr3)
statement;
contoh:<?php/* example 1 */
for ($i = 1; $i <= 10; $i++) { echo $i;}
/* example 2 */
for ($i = 1; ; $i++) { if ($i > 10) { break; } echo $i;}
/* example 3 */
$i = 1;for (; ; ) { if ($i > 10) { break; } echo $i; $i++;}
Top Related