1 / 14

PHP Flow Control

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>” ; } ?>.

conner
Download Presentation

PHP Flow Control

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP Flow Control

  2. if • Almost identical to C <?php if ($intM > $intG){ echo “<p>$intM is greater</p>”; } ?>

  3. If else • Almost identical to C <?php if ($intM > $intG){ echo “<p>$intM is greater</p>”; } else { echo “<p>$intG is greater</p>”; } ?>

  4. 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>”; } ?>

  5. 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>”; • }

  6. 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...

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

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

  9. 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>"; }

  10. 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>"; }

  11. 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>"; }

  12. break • $intA = 1; • while (1) { • //do something • $intA++; • if ($intA > 10) break; • }

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

  14. Demo • EasyPHP • GET • POST • REQUEST • Superglobals • File manipulations

More Related