PHP Flow Control if Almost identical to C

  • Slides: 14
Download presentation
PHP Flow Control

PHP Flow Control

if Almost identical to C <? php if ($int. M > $int. G){ echo

if Almost identical to C <? php if ($int. M > $int. G){ echo “<p>$int. M is greater</p>”; } ? >

If else Almost identical to C <? php if ($int. M > $int. G){

If else Almost identical to C <? php if ($int. M > $int. G){ echo “<p>$int. M is greater</p>”; } else { echo “<p>$int. G is greater</p>”; } ? >

If elseif else PHP <? php if ($int. M > $int. G){ echo “<p>$int.

If elseif else PHP <? php if ($int. M > $int. G){ echo “<p>$int. M is greater</p>”; } elseif ( $int. M < $int. G) { echo “<p>$int. G is greater</p>”; } else { echo “<p>$int. G is equal to $int. M</p>”; } ? >

switch ($str. A) { case “Russell”: echo “<p> Hi Russell </p>”; break; case “Kevin”:

switch ($str. A) { case “Russell”: echo “<p> Hi Russell </p>”; break; case “Kevin”: echo “<p> Hi Kevin</p>”; break; case “Dug”: echo “<p> Hi Dug</p>”; break; default: echo “<p> I don't know you. . . </p>”; }

while $int. A = 0; while ($int. A < 11) { echo "<p>now the

while $int. A = 0; while ($int. A < 11) { echo "<p>now the value of ". '$int. A'. " is $int. A</p>"; $int. A++; } Notice the use of “ and ' The first $int. A in the echo statement does not get a numerical value. . .

do-while do { echo"<p>now the value of ". '$int. A'. " is $int. A</p>";

do-while do { echo"<p>now the value of ". '$int. A'. " is $int. A</p>"; $int. A++; } while ($int. A < 21);

for loops Similar to C: e. g. , for ($int. A = 1; $int.

for loops Similar to C: e. g. , for ($int. A = 1; $int. A <=10; $int. A++) {. . . }

foreach (first form) Also a special construct: foreach (array as value) statement $arr. Num

foreach (first form) Also a special construct: foreach (array as value) statement $arr. Num = array (0=>"zero", 1=>"one", 2=>"two", 4=>"four", 3=>"three", 5 =>"five"); $int. Count = 0; foreach($arr. Num as $str. Num){ echo "<p>". $int. Count++. "$str. Num</p>"; }

foreach (second form) foreach (array as key=> value) statement $arr. Num = array (0=>"zero",

foreach (second form) foreach (array as key=> value) statement $arr. Num = array (0=>"zero", 1=>"one", 2=>"two", 4=>"four", 3=>"three", 5 =>"five"); $int. Count = 0; foreach($arr. Num as $int. Key=>$str. Num){ echo "<p> $int. Key $str. Num</p>"; }

foreach (second form) foreach (array as key=> value) statement Non-numerical keys $arr. Num =

foreach (second form) foreach (array as key=> value) statement Non-numerical keys $arr. Num = array ("0"=>"zero", "1"=>"one", "2"=>"two", "4"=>"four", "3"=>" three", "5"=>"five"); $int. Count = 0; foreach($arr. Num as $str. Key=>$str. Num){ echo "<p> $str. Key $str. Num</p>"; }

break $int. A = 1; while (1) { //do something $int. A++; if ($int.

break $int. A = 1; while (1) { //do something $int. A++; if ($int. A > 10) break; }

continue for($int. A=1; $int. A<10; $int. A++){ if($int. A % 2){continue; } echo "<p>print

continue for($int. A=1; $int. A<10; $int. A++){ if($int. A % 2){continue; } echo "<p>print even number } $int. A</p>";

Demo • Easy. PHP • GET • POST • REQUEST • Superglobals • File

Demo • Easy. PHP • GET • POST • REQUEST • Superglobals • File manipulations