1 / 15

PHP Selection & Looping Statements

PHP Selection & Looping Statements. Control Structures. Programming Languages commonly include a number of “control structures”, that allow users to execute based on evaluation. Blocks of statements are enclosed in curly brackets ({}). PHP includes the following control structures: IF-ELSE

wyatt
Download Presentation

PHP Selection & Looping Statements

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 Selection & Looping Statements

  2. Control Structures Programming Languages commonly include a number of “control structures”, that allow users to execute based on evaluation. Blocks of statements are enclosed in curly brackets ({}). PHP includes the following control structures: • IF-ELSE • WHILE • FOR • SWITCH

  3. Conditional Operators

  4. Conditional Operators

  5. Control Structure #1IF-ELSE *NOTE: the else is optional

  6. if Statements if ($number > 10) echo “That is a big number”; if ($number > 10) { echo “<BR> The number is greater than 10.”; echo “<BR> So, it must be big.”; } if ($number > 10) echo “<BR> That’s a big number.”; else echo “<BR> That’s a small number.”;

  7. if Statements if ($number > 10) { echo “<BR> The number is greater than 10.”; echo “<BR> So, it must be big.”; } else { echo “<BR> The number is less or equal to 10.”; echo “<BR> So, it must be small.”; }

  8. if Statements if ($number > 10) if ($number > 100) echo “<BR>That is a very big number.”; else { echo “<BR> The number is greater than 10.”; echo “<BR> So, it must be big.”; } else echo “<BR>That is a small number.”;

  9. if Statements if ($number > 100) echo “<BR>That’s a very big number.”; elseif ($number > 10) echo “<BR>That is a big number.”; elseif ($number > 1) echo “<BR> That is a small number.”; else echo “<BR> That is a very small number.”;

  10. Control Structure #2SWITCH

  11. switch Statement switch ($number) { case 1: echo “small”; break; case 2: echo “medium”; break; case 3: echo “large”; break; default: echo “That is not a valid code.”; }

  12. Control Structure #3FOR

  13. for statement $sum = 0; for ($n=1; $n<=3; $n++) $sum += $n; echo “<BR> The sum of the integers from 1 to $number is $sum.”;

  14. Control Structure #3WHILE NOTE: $timeleft- - is equivalent to $timeleft = $timeleft -1

  15. while statement $n = 0; $sum = 0; while ($n <=3) { $sum = $sum + $n; $n++; } echo “The sum is $sum”;

More Related