150 likes | 332 Views
PHP Flow Control. if. Almost identical to C <?php if ( $intM > $intG ){ echo “<p> $intM is greater</p>” ; } ?>. If else. Almost identical to C <?php if ( $intM > $intG ){ echo “<p> $intM is greater</p>” ; } else { echo “<p> $intG is greater</p>” ; } ?>.
E N D
if • Almost identical to C <?php if ($intM > $intG){ echo “<p>$intM is greater</p>”; } ?>
If else • Almost identical to C <?php if ($intM > $intG){ echo “<p>$intM is greater</p>”; } else { echo “<p>$intG is greater</p>”; } ?>
If elseif else • PHP <?php if ($intM > $intG){ echo “<p>$intM is greater</p>”; } elseif ( $intM < $intG) { echo “<p>$intG is greater</p>”; } else { echo “<p>$intG is equal to $intM</p>”; } ?>
switch • switch ($strA) { • 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 • $intA = 0; • while ($intA < 11) { • echo "<p>now the value of ". '$intA' ." is $intA</p>"; • $intA++; • } • Notice the use of “ and ' • The first $intA in the echo statement does not get a numerical value...
do-while • do { • echo"<p>now the value of " . '$intA' . " is$intA</p>"; • $intA++; • } while ($intA < 21);
for loops • Similar to C: • e.g., for ($intA = 1; $intA <=10; $intA++) { ... }
foreach (first form) • Also a special construct: • foreach (array as value) statement $arrNum = array (0=>"zero", 1=>"one",2=>"two",4=>"four",3=>"three",5=>"five"); $intCount = 0; foreach($arrNum as $strNum){ echo "<p>" . $intCount++ . "$strNum</p>"; }
foreach (second form) • foreach (array as key=> value) statement $arrNum= array (0=>"zero", 1=>"one",2=>"two",4=>"four",3=>"three",5=>"five"); $intCount= 0; foreach($arrNum as $intKey=>$strNum){ echo "<p> $intKey$strNum</p>"; }
foreach (second form) • foreach (array as key=> value) statement • Non-numerical keys $arrNum = array ("0"=>"zero", "1"=>"one","2"=>"two","4"=>"four","3"=>"three","5"=>"five"); $intCount = 0; foreach($arrNum as $strKey=>$strNum){ echo "<p> $strKey $strNum</p>"; }
break • $intA = 1; • while (1) { • //do something • $intA++; • if ($intA > 10) break; • }
continue • for($intA=1; $intA<10; $intA++){ • if($intA % 2){continue;} • echo "<p>print even number $intA</p>"; • }
Demo • EasyPHP • GET • POST • REQUEST • Superglobals • File manipulations