| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | --- | 
					
						
							| 
									
										
										
										
											2018-12-29 16:24:19 -05:00
										 |  |  | title: Loops | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-29 16:24:19 -05:00
										 |  |  | # PHP Loops
 | 
					
						
							|  |  |  | When you need to repeat a task multiple times, you can use a loop instead of adding the same code over and over again. | 
					
						
							|  |  |  | PHP has the following loop statements : | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | - for - loop through a block of code with specific number of times. | 
					
						
							|  |  |  | - while - loop through a block of code if condition is true. | 
					
						
							|  |  |  | - do...while - loop through a block of code one and continue loop if condition is true. | 
					
						
							|  |  |  | - foreach - loop through a block of code for each value within an array. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Using a `break` within the loop can stop the loop execution. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # For loop
 | 
					
						
							|  |  |  | Loop through a block of code with specific number of times. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Syntax
 | 
					
						
							| 
									
										
										
										
											2018-11-18 06:31:26 +02:00
										 |  |  | ```php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | for (init counter; condition; counter increment or decrement) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // Code to be executed | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Example
 | 
					
						
							|  |  |  | ```php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  | for($index = 0; $index < 5; $index ++) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     echo "Current loop counter ".$index.".\n"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ?> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Output
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | > Current loop counter 0.
 | 
					
						
							|  |  |  | > Current loop counter 1.
 | 
					
						
							|  |  |  | > Current loop counter 2.
 | 
					
						
							|  |  |  | > Current loop counter 3.
 | 
					
						
							|  |  |  | > Current loop counter 4.
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # While loop
 | 
					
						
							|  |  |  | Loop through a block of code if condition is true. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Syntax
 | 
					
						
							| 
									
										
										
										
											2018-11-18 06:31:26 +02:00
										 |  |  | ```php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | while (condition) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // Code to be executed | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Example
 | 
					
						
							|  |  |  | ```php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  | $index = 10; | 
					
						
							|  |  |  | while ($index >= 0) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     echo "The index is ".$index.".\n"; | 
					
						
							|  |  |  |     $index--; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ?> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Output
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | > The index is 10.
 | 
					
						
							|  |  |  | > The index is 9.
 | 
					
						
							|  |  |  | > The index is 8.
 | 
					
						
							|  |  |  | > The index is 7.
 | 
					
						
							|  |  |  | > The index is 6.
 | 
					
						
							|  |  |  | > The index is 5.
 | 
					
						
							|  |  |  | > The index is 4.
 | 
					
						
							|  |  |  | > The index is 3.
 | 
					
						
							|  |  |  | > The index is 2.
 | 
					
						
							|  |  |  | > The index is 1.
 | 
					
						
							|  |  |  | > The index is 0.
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Do...While loop
 | 
					
						
							| 
									
										
										
										
											2018-12-30 03:20:25 +05:30
										 |  |  | Loop through a block of code once and continue to loop if the condition is true. | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ## Syntax
 | 
					
						
							| 
									
										
										
										
											2018-11-18 06:31:26 +02:00
										 |  |  | ```php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | do | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // Code to be executed | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | while (condition); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Example
 | 
					
						
							|  |  |  | ```php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  | $index = 3; | 
					
						
							|  |  |  | do | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // execute this at least 1 time | 
					
						
							|  |  |  |     echo "Index: ".$index.".\n";  | 
					
						
							|  |  |  |     $index --; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | while ($index > 0); | 
					
						
							|  |  |  | ?> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Output
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | > Index: 3.
 | 
					
						
							|  |  |  | > Index: 2.
 | 
					
						
							|  |  |  | > Index: 1.
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Foreach loop
 | 
					
						
							|  |  |  | Loop through a block of code for each value within an array. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Syntax
 | 
					
						
							| 
									
										
										
										
											2018-11-18 06:31:26 +02:00
										 |  |  | ```php | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | foreach ($array as $value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // Code to be executed | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Example
 | 
					
						
							|  |  |  | ```php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  | $array = ["Ali", "Ah Kao", "Muthu", "Gwen", "Lucida", "Cecily", "Arthur", "Flora"]; | 
					
						
							|  |  |  | foreach ($array as $name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     echo "Hi, my name is ".$name.".\n";  | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     if ($name == "Cecily") | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         echo "\"Hello, ".$name."!\""; | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         // stop the loop if name is Cecily | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ?> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Output
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | > Hi, my name is Ali.
 | 
					
						
							|  |  |  | > Hi, my name is Ah Kao.
 | 
					
						
							|  |  |  | > Hi, my name is Muthu.
 | 
					
						
							|  |  |  | > Hi, my name is Gwen.
 | 
					
						
							|  |  |  | > Hi, my name is Lucida.
 | 
					
						
							|  |  |  | > Hi, my name is Cecily.
 | 
					
						
							|  |  |  | > "Hello, Cecily!"
 | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2018-12-29 23:24:53 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | ## For More Information:
 | 
					
						
							|  |  |  | http://php.net/manual/en/control-structures.for.php |