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

The loop statement: for, do...while, foreach and we add break - continue statement in PHP.


<< Previous Page
PHP While Loops
Next Page >>
PHP Array


PHP Tutorial » For do while foreach break continue in php

Loops in PHP are used to execute the same block of code a specified number of times. We are discussing PHP supports following four loop types: for, while, do...while, foreach.
We will discuss about continue and break keywords used to control the loops execution.

For loop in PHP statement.

Syntax

for (initialization; condition; increment)
{
code to be executed;
}

Example of statement - for loop PHP-

<?php
$a = 0;
$b = 0;
for( $i = 0; $i<5; $i++ )
{
$a += 10;
$b += 5;
}
echo ("At the end of the loop a = $a and b = $b" );
?>

- While - loop PHP statement

The while statement will execute a block of code if and as long as a test expression is true.
Syntax of - While - loop PHP

while (condition)
{
code to be executed;
}

Example of - While - loop PHP

$i = 0;
$num = 50;
while( $i < 10)
{
$num--;
$i++;
}
echo ("Loop stopped at i = $i and num = $num" );

- do... while - loop PHP statement
Syntax

do {
code to be executed;
}
while (condition);

Example

$i = 0;
$num = 0;
do {
$i++;
}
while( $i < 10 );
echo ("Loo

- foreach - loop statement PHP
Syntax

foreach (array as value) {
code to be executed;
}

Example - foreach - loop statement PHP

$array = array( 1, 2, 3, 4, 5);
foreach( $array as $value )
{
echo "Value is $value";
}
This will produce this result −
Value is 1
Value is 2
Value is 3
Value is 4
Value is 5

- break - statement PHP
The PHP break keyword is used to terminate the execution of a loop prematurely.
Example - break - statement

$i = 0;
while( $i < 10) {
$i++;
if( $i == 3 )break;
}
echo ("Loop stopped at i = $i" );
?>
This will produce the following result − Loop stopped at i = 3

- continue - statement PHP
Example

$array = array( 1, 2, 3, 4, 5);
foreach( $array as $value ) {
if( $value == 3 )continue;
echo "Value is $value";
}
This will produce the following result −
Value is 1
Value is 2
Value is 4
Value is 5

<< Previous Page
PHP While Loops
Next Page >>
PHP Array


for PHP, do...while PHP, foreach PHP, break, continue statement in PHP
The loop statement: for, do...while, foreach and we add break - continue statement in PHP - 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.


Upload Image, display, edit and delete i...
Login Form with Image background...
How to Create an Image with Transparent ...
Portfolio Gallery Website with filtering...
Simple pagination script PHP MySQLi...
Center Image in div...
Image Hover Overlay Fade...
Sticky image / element / div on scroll...
Responsive images...
Create rounded image in HTML CSS...
Add border around image...
Position Text Over an Image HTML CSS res...
Create a Slideshow Images Gallery...
Create a Sticky Sidebar...
Search bar using CSS HTML...
Shrink Navigation Menu on Scroll...
How to Create Accordion HTML Templates...
Dropdown menu in the navigation bar...
Responsive Top Navigation Bar with mobil...
Split horizontal navigation bar...