AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

PHP for loop


<< Previous Page
PHP tutorial
Next Page >>
PHP tutorial

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:

<?php
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:

Example (this is HTML - PHP editor, change text on this window)

<?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:

Example (this is HTML - PHP editor, change text on this window)
<?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

Example (this is HTML - PHP editor, change text on this window)
<?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

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Home
PHP Introduction
PHP Install
PHP Syntax
PHP Variables
PHP Echo and Print
PHP Data Types
PHP String Function
PHP Constants
PHP Operator Types
PHP If Else and Elseif
PHP Switch
PHP While Loops
PHP Loop For Do Foreach
PHP Array
Convert Array to String
PHP Function
PHP GET and POST
PHP Date/Time Functions
PHP Login
PHP Delete Element
PHP eregi_replace()
PHP mysql_query()
PHP Errors to Display
PHP Loop For Do Foreach

PHP forms
PHP Form Example
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.