PHP Iterative Statements
PHP Iterative Statements
Iterative statements are used to run same block of code over and over again for a certain number of times.
In PHP, we have the following loops:
- while Loop
- do-while Loop
- for Loop
- foreach loop
1. While Loop
The While loop in PHP is used when we need to execute a block of code again and again based on a given condition. If the condition never becomes false, the while loop keeps getting executed. Such a loop is known as an infinite loop.
Example:
2. Do-While Loop
The do-while loop is similar to a while loop except that it is guaranteed to execute at least once. After executing a part of a program for once, the rest of the code gets executed based on a given boolean condition.
Example:
3. For Loop
The for loop is used to iterate a block of code multiple times.
Example:
4. Foreach loop
The foreach loop in PHP can be used to access the array indexes in PHP. It only works on arrays and objects.
Example: