use of loop in PHP
When you want the same block of code to run over and over again in a row. Instead of adding several almost equal lines in a script we can use loops to perform a task.
When you want to execute same statements more than one times then use loop.
Four types of Loop used in PHP :
- for : In for loop specified condition is predefined.
- while : while loop executes the statement while a specified condition is true.
In while loop first check the condition then execute the statement.
- do-while : do – while loop executes the statement once and next statement depends upon a specified condition is true.
In do – while loop first execute the statement then check the condition, it means if condition is false in that case one time statement execute.
- foreach : Foreach loop executes the statement() of an associative array.