HOME | PHP Tutorial | CSS
facebook icon
AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP
HTML Tutorial

CSS Tutorial

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

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

3357. PHP If Statements
Learn about PHP If statements with this PHP tutorial.
https://www.quackit.com/php/tutorial/php_if_statements.cfm

3354. PHP For Loops
Learn about PHP for loops with this PHP tutorial.
https://www.quackit.com/php/tutorial/php_for_loops.cfm

3226. PHP While Loops. Tutorial While Loop, php tutorial
PHP While Loops. Tutorial While Loop php tutorial
https://www.agernic.com/php-tutorial/php-while-loops.html

3216. PHP 5 Constants, php tutorial
PHP 5 Constants php tutorial
https://www.agernic.com/php-tutorial/php-constants.html

2254. PHP Tutorial - Variables
PHP variables are just a name for a value and you can simply use it like this $var_name = value;. They usually store sting caracters and numeric values for later use in the programming block.
https://tutorialehtml.com/en/php-tutorial-variables/

3278. PHP Date / Time Functions - how to create? - example, php tutorial
PHP Date / Time Functions - how to create? - example php tutorial
https://www.agernic.com/php-tutorial/php--date-time-functions-how-to-create-example.html

Your add here
How to get PHP errors to display?
How to prevent sql injection in php mysqli
How do I get PHP errors to display - PHP error_reporting()
mysql_query(): The mysql extension is deprecated and will be removed i
Function eregi_replace() is deprecated in php - How to fix
PHP delete an element from an array
Uncaught Error: Call to undefined function MYSQL_NUM_ROWS() in
Deprecated: mysql_connect() mysql extension is deprecated and will be
mysqli_fetch_array() expects parameter 1 to be mysqli_result boolean g
SQL IN Operator - example
SQL BETWEEN Operator
SQL like syntax and example
SQL WHERE AND & OR multiple conditions
SQL create table primary key autoincrement
PHP mysqli_connect () Function
SQL COUNT(), AVG() and SUM() Functions
Display data from SQL database with MySQLi into php - html table
SQL CREATE DATABASE - example
SQL mysql order by columns, by date, by asc, by desc, random with PHP
PHP mysqli_query() Function - mysqli query
SQL delete row - SQL delete table
SQL update - SQL update select
Insert into SQL - Insert into table SQL - Insert data in sql
Login in PHP - simple login script with PHP MYSQL