Tutorial by Examples

for

The for statement is used when you know how many times you want to execute a statement or a block of statements. The initializer is used to set the start value for the counter of the number of loop iterations. A variable may be declared here for this purpose and it is traditional to name it $i....
The foreach statement is used to loop through arrays. For each iteration the value of the current array element is assigned to $value variable and the array pointer is moved by one and in the next iteration next element will be processed. The following example displays the items in the array a...
The break keyword immediately terminates the current loop. Similar to the continue statement, a break halts execution of a loop. Unlike a continue statement, however, break causes the immediate termination of the loop and does not execute the conditional statement again. $i = 5; while(true) {...
The do...while statement will execute a block of code at least once - it then will repeat the loop as long as a condition is true. The following example will increment the value of $i at least once, and it will continue incrementing the variable $i as long as it has a value of less than 25; $i...
The continue keyword halts the current iteration of a loop but does not terminate the loop. Just like the break statement the continue statement is situated inside the loop body. When executed, the continue statement causes execution to immediately jump to the loop conditional. In the followin...
The while statement will execute a block of code if and as long as a test expression is true. If the test expression is true then the code block will be executed. After the code has executed the test expression will again be evaluated and the loop will continue until the test expression is foun...

Page 1 of 1