Introduction to Control Statements in PHP

Estudies4you
Introduction to Control Statements in PHP

Control Statements - Introduction

PHP also provides control statements or loop statements.
These are:
➤for
➤while
➤do-while


for Statement
Syntax Example
for (initial; condition; increment)
{
statement to be executed as specified in condition
}

Example
<?php
for ($p=0; $p<=10; $p++)
{
echo
"NUMBER PRINTED " .$p."<br>" ;
}
?>

OUTPUT:

while Statement
Syntax Example
while (condition)
{
statement to be executed
}



Example
<?php
$p=0;
while($p<=10)
{
echo "Number printed is " .$p. "<br>";
$p++;
}
?>

OUTPUT:

do while Statement
Syntax
do
{
Statement to be executed;
} while (condition);
               
Example
<?php
$p=0;
do
{
$p++;
echo "Number printed is" .$p. "<br>";
while ($p<=10);
?>


OUTPUT:


To Top