PHP Menu


Control Structures in PHP


The Control Structures (or) Statements used to alter the execution process of a program.

These statements are mainly categorized into following:

  • Conditional Statements
  • Loop Statements
  • Jump Statements

Conditional Statements :

Conditional Statements performs different computations or actions depending on conditions. In PHP, the following are conditional statements

  • if statement
  • if - else statement
  • if - elseif - else statement
  • switch statement

if statement

The if statement is used to test a specific condition. If the condition is true, a block of code (if-block) will be executed.

Syntax :

if (condtion)
{
	statements
}

if - else statement

The if-else statement provides an else block combined with the if statement which is executed in the false case of the condition.

Syntax :

if (condtion)
{
	statements
}
else
{
	statements
}

if - elseif - else statement

The elseif statement enables us to check multiple conditions and execute the specific block of statements depending upon the true condition among them.

Syntax :

if (condtion1)
{
	statements
}
else if (condtion2)
{
	statements
}
else if (condtion3)
{
	statements
}
.
.
else
{
	statements
}


Example: "ConditionalDemo.php"


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

Output :

image

switch statement

The switch statement enables us to execute a block of code from multiple conditions depending upon the expression.

Syntax :

switch (expression)
{
case 1: statements
		break;
case 2: statements
		break;
case 3: statements
		break;
.
.
default: statements
}


Example: "SwitchDemo.php"


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

Output :

image

Loop Statements :

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

  • while loop
  • do - while loop
  • for loop

while loop statement

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
}

Example: "WhileDemo.php"


<html>

<head>
<title>While Demo</title>
</head>
<body>
<h1>While Demo</h1>
<?php
$n
=1;
while(
$n<=5)
{
    echo 
"$n <br/>";
    
$n++;
}
?>
</body>
</html>

Output :

image

do - while loop statement

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);

Example: "DoWhileDemo.php"


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

Output :

image

for loop statement

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
}

Example: "ForDemo.php"


<html>

<head>
<title>For Demo</title>
</head>
<body>
<h1>For Demo</h1>
<?php
for($i=1;$i<=5;$i++)
{
    echo 
"$i <br/>";
}
?>
</body>
</html>

Output :

image

Jump Statements :

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

  • break statement
  • continue statement

break statement

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;

Example: "BreakDemo.php"


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

Output :

image

continue statement

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;

Example: "ContinueDemo.php"


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

Output :

image
Next Topic :Arrays in PHP