In programming, a loop consists of a series of instructions that are repeatedly executed until a condition is met. Loops are required in PHP because sometimes we need to execute the same block of code again and again in a program until a certain condition is met. Hence, instead of writing several lines of repetitive code, we can use a loop.
A loop attempts to automate repetitive tasks within a program because we need to save both time and effort. PHP supports the following four different types of loops.
- While — This command enables us to loop through a block of code until a specified condition is true.
- Do…while — Using the do...while statement a block of code can be executed once unconditionally and then a condition is evaluated. If the condition is true then the block of code will be repeatedly executed as long as the specified condition is true.
- For — This loops through a block of PHP code a specified number of times which is mentioned in the statement.
- Foreach — This repeatedly executes a block of PHP code for every element in an array.
The while loop in PHP
Using the while statement a block of PHP code will be repeatedly executed as long as the condition in the while statement evaluates to true. It is called the while statement because the block of code is executed while the condition is true.
Code:
The example below shows a loop that starts at $a=1. This loop will continue to run as long as variable $a is less than or equal to 5. Hence the loop will be executed 5 times. Every time the loop executes $a will be increased by 1 and the condition will be checked at the start of the next loop.
Example:
Code:
do…while Loop in PHP
The do...while statement is a deviation of the while loop, which evaluates the condition at the end of each loop iteration.
The do...while loop executes a block of code at least once unconditionally, and then repeats the loop (i.e. executes the statements in the loop)as long as the specified condition evaluates to true.
Code:
The example below shows the implementation of a do…while loop. The variable whose value will be evaluated in the condition is initialized to 5. Everytime that the loop executes $a is incremented by one and the value of $a is displayed. The condition is evaluated at the end of the loop execution because of which the loop executes at least once. The loop will continue to run as long as $a is less than, or equal to 8. Hence the loop executes a total of 4 times.
Example:
Code:
Difference between while and do…while Loop
In the While loop, the condition is evaluated at the beginning of each loop iteration. If the conditional expression evaluates to false at the start itself, then the loop will never be executed.
Using the do…while loop the loop will execute at least once, even when the conditional expression is false because the condition is evaluated at the end of the loop iteration rather than at the beginning.
The For loop in PHP
The for loop repeatedly executes a block of code until the specified condition is met. Because of this it is usually used when we already know the number of times that the block of code needs to be repeatedly executed.
Code:
The for statement syntax consists of the following 3 parts each of which has a specific meaning
- Initialization – It is so called because it is used to initialize the counter variables, and it is executed just once unconditionally before the body of the loop is executed for the first time.
- Condition - At the beginning of every iteration, the condition is evaluated. If the condition is true, the loop continues and executes the nested statements within the for the loop. If the condition is false, the execution of the loop ends because the condition fails.
- Increment - This will update the loop counter with a new value based on the operation specified. This will be executed at the end of every iteration. It is so called because the operation usually performed is the increment operation. However, any other arithmetic operations could also be performed.
The example below defines a loop that starts at $a=1. The loop will continue to execute until $a is less than, or equal to 5 because of the condition which is specified. The variable $a will increase by 1 each time the loop executes because of the increment part of the for loop statement. Hence the loop will execute 5 times.
Example:
Code:
PHP foreach Loop
The foreach loop is used to iterate over arrays. It is called as a foreach loop because it enables us to execute a set of statements for every element of an array.
Code:
The example below demonstrates a loop that will print the values of the given array:
Example:
Code:
There is another syntax of the foreach loop (which is an extension of the first) because we need to handle different types of arrays.
Code:
Example:
Code:
Here the echo statement is executed for every element of the array $winner