PHP Theory
Loops
Loops are used to executed a set of statements repeatedly until a particlar condition is satisfied.
PHP supports four loop types:
1. Entry controlled loop(for loop, while loop)
2. Exit controlled loop(do-while loop )
3. Exit controlled loop(foreach loop)
For Loop
For Loop
It execute a sequence of statements multiple times. A for loop has three arguments, namely, initialization, test-condition, and increment/decrement variable.
Syntax |
---|
for(initialization statement; test condition; increment/decrement statement) { //statement to be executed } |
While Loop
While Loop
In while loop, initialization condition is evaluated first and if it returns true then the statements inside while loop execute. When condition returns false, it comes out of the loop. It tests the increment/decrement condition before executing the loop body.
Syntax |
---|
initialization statement; while (test-condition) { //statements to be executed increment/decrement statement; } |
Do While
Do-While Loop
Do While loop is considered to be a conditional statement completely similar to a normal While loop. The only difference is that in do while loop except that it tests the condition at the end of the loop body. It loops through a block of code once, and then repeats the loop as long as the specified condition is true.
Syntax |
---|
initialization statement; do { //statements to be executed increment/decrement statement; } while (test-condition); |