The Control Structures (or) Statements used to alter the execution process of a program.
These statements are mainly categorized into following:
Conditional Statements performs different computations or actions depending on conditions. In PHP, the following are conditional statements
if (condtion)
{
statements
}
if (condtion)
{
statements
}
else
{
statements
}
if (condtion1)
{
statements
}
else if (condtion2)
{
statements
}
else if (condtion3)
{
statements
}
.
.
else
{
statements
}
<html>
<head>
<title>Conditional Demo</title>
</head>
<body>
<?php
$x=15;
$y=5;
if ($x > $y)
{
echo "$x is greater than $y";
}
else if ($x < $y)
{
echo "$x is lessthan $y";
}
else
{
echo "Both are Equal";
}
?>
</body>
</html>
switch (expression)
{
case 1: statements
break;
case 2: statements
break;
case 3: statements
break;
.
.
default: statements
}
<html>
<head>
<title>Switch Demo</title>
</head>
<body>
<?php
$x=15;
$y=10;
$op='*';
switch($op)
{
case '+': $z = $x + $y;
echo "Addition is : $z";
break;
case '-': $z = $x - $y;
echo "Subtraction is : $z";
break;
case '*': $z = $x * $y;
echo "Multiplication is : $z";
break;
case '/': $z = $x / $y;
echo "Division is : $z";
break;
case '%': $z = $x % $y;
echo "Modulus is : $z";
break;
default: echo "Invalid Operator";
}
?>
</body>
</html>
Sometimes we may need to alter the flow of the program. If the execution of a specific code may need to be repeated several numbers of times then we can go for loop statements.
In PHP, the following are loop statements
With the while loop we can execute a set of statements as long as a condition is true. The while loop is mostly used in the case where the number of iterations is not known in advance.
Syntax :
while (condition)
{
statements
}
<html>
<head>
<title>While Demo</title>
</head>
<body>
<h1>While Demo</h1>
<?php
$n=1;
while($n<=5)
{
echo "$n <br/>";
$n++;
}
?>
</body>
</html>
The do-while loop will always execute a set of statements atleast once and then execute a set of statements as long as a condition is true.
Syntax :
do
{
statements
} while (condition);
<html>
<head>
<title>Do-While Demo</title>
</head>
<body>
<h1>Do-While Demo</h1>
<?php
$n=1;
do
{
echo "$n <br/>";
$n++;
} while($n<=5);
?>
</body>
</html>
With the for loop we can execute a set of statements specified number of times. The for loop is mostly used in the case where the number of iterations is known in advance..
Syntax :
for (initialization; condition; increment/decrement)
{
statements
}
<html>
<head>
<title>For Demo</title>
</head>
<body>
<h1>For Demo</h1>
<?php
for($i=1;$i<=5;$i++)
{
echo "$i <br/>";
}
?>
</body>
</html>
Jump statements in PHP are used to alter the flow of a loop like you want to skip a part of a loop or terminate a loop.
In PHP, the following are jump statements
The break is a keyword in php which is used to bring the program control out of the loop. i.e. when a break statement is encountered inside a loop, the loop is terminated and program control resumes at the next statement following the loop.
The break statement breaks the loops one by one, i.e., in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops. The break is commonly used in the cases where we need to break the loop for a given condition.
Syntax :
break;
<html>
<head>
<title>Break Demo</title>
</head>
<body>
<h1>Break Demo</h1>
<?php
for($i=1;$i<=10;$i++)
{
if($i==5)
{
break; //terminates the current loop
}
echo "$i <br/>";
}
echo "Loop is Over !";
?>
</body>
</html>
The continue statement in php is used to bring the program control to the beginning of the loop. i.e. when a continue statement is encountered inside the loop, remaining statements are skipped and loop proceeds with the next iteration.
The continue statement skips the remaining lines of code inside the loop and start with the next iteration.
It is mainly used for a particular condition inside the loop so that we can skip some specific code for a particular condition.
Syntax :
continue;
<html>
<head>
<title>Continue Demo</title>
</head>
<body>
<h1>Continue Demo</h1>
<?php
for($i=1;$i<=10;$i++)
{
if($i%2==0)
{
continue; //terminates the current Iteration and moves to Next
}
echo "$i <br/>";
}
echo "Loop is Over !";
?>
</body>
</html>