humanware

php_training_day4_loops

Aug 7th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.23 KB | None | 0 0
  1. <?php
  2. /*
  3. Loops
  4. To do repetitive tasks.
  5. To run block of code certain time until condition meets.
  6.  
  7. Types of Loops
  8. 1. While Loop
  9.     Loops through a block of code until condition is evaluated to true.
  10. 2. Do While Loop
  11.     The block of code executed once and then condition is evaluated. If condition is true, the statement is repeated as long as the specified condition is true
  12. 3. For Loop
  13.     Loops through a block of code until the counter reaches a specified number
  14. 4. Foreach Loop
  15.     Loops through a block of code for each element in an array.
  16.     */
  17.  
  18. // PHP WHILE LOOP
  19. // Syntax
  20. // while (condition) {
  21. //  code to be executed
  22. // }
  23.  
  24. $i = 1;
  25. while ($i <= 3) {
  26.     echo $i . '<br />';
  27.     $i++;
  28. }
  29.  
  30. echo '<br /><br /><br />';
  31.  
  32. /* PHP DO WHILE LOOP
  33. Syntax:
  34. do {
  35.     code to be executed
  36. }
  37. while (condition)
  38. */
  39.  
  40. $i = 1;
  41. do {
  42.     echo $i . '<br />';
  43.     $i++;
  44. }
  45. while ($i <= 10);
  46.  
  47. echo '<br /><br /><br />';
  48.  
  49. /*
  50. FOR LOOP
  51. Syntax:
  52. for (initialization; condition; increment) {
  53.     code to be executed
  54. }
  55. */
  56. for ($i = 10; $i >= 1; $i--) {
  57.     echo 'The number is ' . $i . '<br />';
  58. }
  59.  
  60. echo '<br /><br /><br />';
  61.  
  62. /* FOREACH LOOP
  63. syntax:
  64. indexed array
  65. foreach ($array as $value) {
  66.     code to be executed
  67. }
  68.  
  69. associative array
  70. foreach $array as $key => $value) {
  71.     code to be executed
  72. }
  73. */
  74.  
  75. $colors = array('Green', 'Blue', 'Red', 'White', 'Orange');
  76. foreach ($colors as $color) {
  77.     echo 'The color is ' . $color . '<br />';
  78. }
  79.  
  80. echo '<br /><br /><br />';
  81.  
  82. $superhero = array(
  83.     'name'      => 'Peter Parker',
  84.     'email'     => '[email protected]',
  85.     'age'       => 48
  86. );
  87.  
  88. foreach ($superhero as $key => $value) {
  89.     echo $key . ' : ' . $value . '<br />';
  90. }
  91.  
  92. echo '<br /><br /><br />';
  93.  
  94. $superhero = array(
  95.     'spider-name' => array(
  96.         'name'  => 'Clark Kent',
  97.         'email' => '[email protected]'
  98.     ),
  99.     'super-man' => array(
  100.         'name' => 'Peter Parker',
  101.         'email' => '[email protected]'
  102.     ),
  103.     'iron-man' => array(
  104.         'name' => 'Harry Potter',
  105.         'email' => '[email protected]'
  106.     )
  107. );
  108.  
  109. $keys = array_keys($superhero);
  110. for ($i = 0; $i < count($superhero); $i++) {
  111.     echo $keys[$i] . '{<br />';
  112.     foreach($superhero[$keys[$i]] as $key => $value) {
  113.         echo $key . ' : ' . $value . '<br />';
  114.     }
  115.     echo '}<br />';
  116. }
  117.  
  118. $i = 0;
  119. echo $i;
  120. $i++
Advertisement
Add Comment
Please, Sign In to add comment