PHP for loop
PHP Tutorial » PHP string contains
What is a FOR loop PHP?
The for loop in PHP is used when you know in advance how many times the script should run.
Syntax:
for (initialization; condition; increment)
{
code to be executed;
}
?>
Where:
Initialization: Is used to set a counter.
Condition: Evaluated for each loop iteration. If it evaluates TRUE, the loop continues. If it evaluates FALSE, the loop ends..
Increment: Used to increases the loop counter value.
PHP for loop example
Consider the following examples. All of them display the numbers from 11 through 20:
<?php
/* example 1 */
for ($i = 11; $i <= 20; $i++) {
echo $i. ", ";
}
echo "<br /><br />";
/* example 2 */
for ($i = 11; ; $i++) {
if ($i > 20) {
break;
}
echo $i. ", ";
}
echo "<br /><br />";
/* example 3 */
$i = 11;
for (; ; ) {
if ($i > 20) {
break;
}
echo $i. ", ";
$i++;
}
echo "<br /><br />";
/* example 4 */
for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
?>
Related subjects:
PHP foreach
What is an array
Convert array to string comma
Array to comma separated
Tags: What is a FOR loop PHP?
php for loop: array, array length, example, break, continue, in html, increment by 10, increment by 2, index, associative array
PHP for loop break
The break loop statement can be used to jump out of a loop.
This example jumps out of the for loop when my_number is equal to 15:
<?php
for ($my_number = 9; $my_number < 20; $my_number++) {
if ($my_number == 15) {
break;
}
echo "My number is: $my_number <br>";
}
?>
PHP for loop continue
The for loop continue statement breaks one iteration in the loop, if a specified condition occurs, and continues with the next iteration in the loop.
The example below does not display My number 16
<?php
for ($my_number = 12; $my_number < 22; $my_number++) {
if ($my_number == 16) {
continue;
}
echo "My number is: $my_number <br>";
}
?>
What is a FOR loop PHP? php for loop: array, array length, example, break, continue, in html, increment by 10, increment by 2, index, associative array
PHP for loop - php tutorial
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.