Flow Control Statements in PHP

Flow control statements in PHP are those statements that change the actual flow of the program code execution during runtime. We can execute a set of statements multiple times or skip a set of statements based on some condition.

In PHP we have several types of flow control statements. First we will start with if-else statements.

If-Else Statement in PHP

If block starts with a particular condition that returns a Boolean value based on the the result of that expression which is given in the condition the block of code may or may not execute. In case the condition evaluated to true than if block gets executed otherwise else part of the code gets executed. We can also have if statements without else part it is not mandatory to include else part with every if statement.

if else statement flowchart - Flow Control Statements in PHP
If-Else Statement Flow Chart

Let’s see a simple example explaining the if-else statement. In the following program we are checking a variable value if it is greater than a specified value than we print a message otherwise we will print some other message.

<?php
    $salary = 10000;
    
    if($salary >= 10000)
        echo "<h1>Your salary is greater than 10000</h1>";
    else
        echo "<h1>Your salary is less than 10000</h1>";
?>

Output

Your salary is greater than 10000

If-ElseIf Statement in PHP

We can also check multiple conditions based on which we can execute different code blocks. For example in the following program we are checking if the salary is greater than some specific value based on that we are printing some message in the output.

if else if flow chart in php - Flow Control Statements in PHP
If Else If Flowchart in PHP
<?php
    $salary = 20000;
    
    if($salary >= 20000)
        echo "<h1>Your salary is greater than 20000</h1>";
    elseif ($salary >= 10000)
        echo "<h1> Your salary is greater than 10000</h1>";
    else
        echo "<h1>Your salary is less than 10000</h1>";
?>

Output

Your salary is greater than 20000

Nested if Blocks in PHP

We can also next if block inside another if block. So first the outer if block condition is checked if it comes out to be true than inner if block will be executed. So we can nest if blocks up-to any level while writing code.

Nested If Else Flowchart in PHP - Flow Control Statements in PHP
Nested If Else Flowchart in PHP
<?php
    $nationality = "Indian";
    $age = 10;
    if($nationality == "Indian")
    {
        if($age >= 18)
        {
            echo "<h1>You are eligible to vote</h1>";
        }
        else
        {
            echo "<h1>Your age is less than 18.So your are not eligible";
        }
    }
    else
    {
        echo "<h1>You are not eligible to vote</h1>";
    }
?>

Output

Your age is less than 18.So your are not eligible

Switch Statement in PHP

We can make use of switch statement to make our task easier if we want to check for multiple conditions rather than using if-elseif statement.

In Switch statement we can pass any character,number or string value and can execute different set of code based on the value of the variable being passed to it. Its not mandatory to pass a boolean expression to switch statement.

Every block of code ends with break statement if we do not include break at the end of code block the control flow will skip to the next case and keeps on executing unless and until it finds the break statement.

In case none of the switch case matches than default block will be executed. Their can be only one default block in a switch statement.

<?php
    $num=10;
    
    switch($num)  
    {
        case 10:
            echo "The number is 10";
            break;
        case 20:
            echo "The number is 20";
            break;
        default:
            echo "The number does not match any of the given conditions";
    }
    
?>

Output

The number is 10

Switch statement is fall through as explained earlier if the break statement is not found in any case block than the flow of code will jump to next case block until it founds the break statement even if the condition of the following case blocks matched or not.

<?php
    $num=20;
    
    switch($num)  
    {
        case 10:
            echo "The number is 10";
            break;
        case 20:
            echo "The number is 20<br/>";
        case 30:
            echo "The number is 30<br/>";
        default:
            echo "The number does not match any of the given conditions";
    }
    
?>

Output

The number is 20
The number is 30
The number does not match any of the given conditions

Loops in PHP

Loops are used in PHP to execute a set of statements repeatedly. Different types of loops that we can make use of in PHP are:

For loop

It is used when we want to execute code a specific number of times. We can start by initializing a variable and increment its value and check its value during each iteration to check whether to continue execution of the loop or not.

For Loop Flowchart in PHP - Flow Control Statements in PHP
For Loop Flowchart in PHP
<?php
    
    for($i=1;$i<=10;$i++)
    {
        echo $i." ";
    }
?>

Output

1 2 3 4 5 6 7 8 9 10

While loop

This loop is used to execute a set of statements unless and until a specific condition does not evaluate to false.

While Loop Flowchart in PHP - For Loop Flowchart in PHP
While Loop Flowchart in PHP
<?php
    $num=10;

    while($num>=1)
    {
        echo $num." ";
        $num--;
    }
?>

Output

10 9 8 7 6 5 4 3 2 1

Do while loop

Do while loop is used when you want to execute the code at least once. The loop condition will not be checked during the first execution. The loop will terminate when the condition evaluates to false.

<?php
    $num=10;

    do
    {
        echo $num." ";
        $num--;
    }while($num!=0);
?>

Output

10 9 8 7 6 5 4 3 2 1

Foreach loop

foreach is used to loop through array elements.

<?php
    $num=array(10,20,30,40);

    foreach($num as $num)
    {
        echo $num." ";
    }    
   
?>

Output

10 20 30 40

Break Statement in PHP

While programming their may arise a need when we need to terminate the loop before it completely executes based on some condition. We can make use of break statements for that.

Break Statement in PHP - Flow Control Statements in PHP
Break Statement in PHP
<?php
    $num=array(10,20,30,40);

    foreach($num as $num)
    {
        echo $num." ";
        if($num == 30)
            break;
    }    
   
?>

Output

10 20 30

Continue Statement in PHP

Continue statement is used when we want to skip current iteration of the loop based on some condition.

Continue Statement in PHP - Flow Control Statements in PHP
Continue Statement in PHP
<?php
    $num=array(10,20,30,40);

    foreach($num as $num)
    {
       if($num == 30)
            continue;
        echo $num." ";
       
    }    
?>

Output

10 20 40

Vatan Bansal

I am a Software Developer by profession. I have been in the field of Software Development from last ten years and has started this blog to share my knowledge on programming as well as WordPress related information like plugins and themes. Please let me know if you want me to write on some specific topic i will try me best to write a post for you.

2 thoughts on “Flow Control Statements in PHP

  • March 22, 2020 at 11:15 am
    Permalink

    Long time supporter, and thought I’d drop a comment.

    Your wordpress site is very sleek – hope you don’t mind me asking what theme you’re using?
    (and don’t mind if I steal it? :P)

    I just launched my site –also built in wordpress like
    yours– but the theme slows (!) the site down quite a bit.

    In case you have a minute, you can find it by searching for “royal cbd”
    on Google (would appreciate any feedback) – it’s still in the works.

    Keep up the good work– and hope you all take care of
    yourself during the coronavirus scare!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *