Guest User

Untitled

a guest
Dec 11th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * for loops
  5. * where to start; when to continue; how to step through
  6. **/
  7.  
  8. for($i = 0; $i < 10; $i++){
  9. // start at i = 0 and continue until i is >= 10
  10. echo $i;
  11. // you would expect: 0123456789
  12. }
  13.  
  14. /**
  15. * while loops
  16. * execute this code while this is true
  17. **/
  18.  
  19. $i = 0; # remember to set these variables if you're using a while loop involving numbers
  20. $j = 5;
  21.  
  22. while($i < 10 && $j < 10){
  23. // so while i and j are both less than 10, print out the sum of i and j
  24. echo $i*$j . " ";
  25. // you would expect: 0 6 14 24 36
  26. $i++;
  27. $j++;
  28. // REMEMBER to increment the variables inside the conditional
  29. // or else you would be stuck in an infinite loop
  30. }
  31.  
  32. /**
  33. * do-while loops
  34. * do the following code, while this is true
  35. **/
  36.  
  37. $i = 0; # remember to set this variable if you're using a do-while involving numbers
  38.  
  39. do {
  40. echo $i;
  41. // you would expect 0123456789
  42. $i++;
  43. } while($i < 10);
  44.  
  45. /**
  46. * breaking out of a loop
  47. **/
  48.  
  49. for($i = 0; $i < 10; $i++){
  50. if($i == 5){
  51. // whenever i is equal to 5, stop the loop and just break out of it!
  52. break;
  53. }
  54.  
  55. echo $i;
  56. // expected 01234
  57. }
  58.  
  59. ?>
Add Comment
Please, Sign In to add comment