×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: William Davies
Added: Dec 5, 2014 5:51 PM
Modified: Dec 5, 2014 5:53 PM
Views: 12
Tags: no tags
while - loops through a block of code as long as the specified condition is true

do while - loops through a block of code once, and then repeats the loop as long as the specified condition is true

for - loops through a block of code a specified number of times

fore­ach - loops through a block of code for each element in an array
  1. for loop
  2. for ($x = 5; $x <= 100; $x++) {
  3.    echo "The number is " . $x;
  4. }
  5. (The first parameter is the initial value of the variable, the second value controls how many times the loop will execute)
  6.  
  7. foreach loop
  8. $myGadgets= array('iMac', 'iPad', 'iPhone');
  9. foreach ($myGadgets AS $myGadget){
  10. echo '<li>' . $myGadget . '</li>';
  11. }
  12. (Outputs each element of the array as a list item)
  13.  
  14. while loop
  15. $x = 1;
  16. while(x <= 5){
  17. echo "Hello World";
  18. x++;
  19. }
  20. (Loop continues until condition is no longer true)
  21.  
  22. *do while loop
  23. $x = 1;
  24. do {
  25.     echo "Hello World";
  26.     $x++;
  27. } while ($x <= 5);
  28. (Identical to a while loop except the code is executed once regardless of if the condition is met)