Loops offer a quick and easy way to do something repeatedly.
Often when you write code, you want the same block of code to run over and over again a certain number of times. So, instead of adding several almost equal code-lines in a script, we can use loops.
Loops are used to execute the same block of code again and again, as long as a certain condition is true.
In PHP, we have the following loop types:
1.while - loops through a block of code as long as the specified condition is true 2.do…while - loops through a block of code once, and then repeats the loop as long as the specified condition is true 3.for - loops through a block of code a specified number of times
A for loop repeats until a specified condition evaluates to false. The JavaScript for loop is similar to the Java and C for loop.
A for statement looks as follows:
for ([initialExpression]; [conditionExpression]; [incrementExpression]) statement
### while statement A while statement executes its statements as long as a specified condition evaluates to true. A while statement looks as follows:
while (condition) statement If the condition becomes false, statement within the loop stops executing and control passes to the statement following the loop.