Table of Contents



Introduction

In this article, we discuss program flow control. The if construct is one of the most important tools of PHP when its come to conditional execution of code fragments. The if construct comes in different flavors:

  • The if statement
  • The if...else statement
  • The if...elseif...else statement

In the following sections, we will dicuss each of these constructs in turn.

The if Statement

Syntax

The if statement.

1if (expr)
2    statement;

The expression expr is evaluated to its Boolean value. If expr evaluates to true, PHP will execute statement, and if it evaluates to false - PHP will ignore it.

If you have a group of statements to execute, you need to enclose the statements within a pair of braces {}.

Syntax

The if statement for a group of statements.

1if (expr){
2    statement1;
3    statement2;
4    ...;
5}

Example

The if statement for a single statement.

1<?php
2    $humidity = 80;
3    if ($humidity > 70) echo "It will rain.";
4?>
It will rain.

Example

The if statement for a group of statements.

1<?php
2    $humidity = 80;
3    if ($humidity > 70){
4        echo "Humidity is $humidity" . "%.<br>";
5        echo "It will rain." . "<br>";
6    }
7?>
Humidity is 80%.
It will rain.

The if...else Statement

Often, we want to execute one set of statement(s) if a certain condition is met, and a different set of statement(s) if the condition is not met. This calls for the if...else construct.

Syntax

The if...else statement.

1if (expr) {
2    statements;
3} else {
4    statements;
5}

If expr evaluates to true, PHP will execute the statements in the first (if) block, and if it evaluates to false, PHP will the statements in the second (else) block.

Example

The if...else statement.

 1<?php
 2    $salary = 8000;
 3    $KPI = 82;
 4    if ($KPI > 80)
 5    {
 6        $bonus = 1000;
 7        $salary += $bonus;
 8    }
 9    else
10    {
11        $bonus = 500;
12        $salary += $bonus;
13    }
14    echo "Your bonus is $bonus. <br>" ;
15    echo "Your updated salary is $salary." ;
16?>
Your bonus is 1000.
Your updated salary is 9000.

ADVERTISEMENT



The if...elseif...else Statement

There are times when we have a number of different possibilities based upon a sequence of conditions. This can be achieved using the if...elseif...else statement.

Syntax

The if...elseif...else statement.

1if (expr1) {
2    statements;
3} elseif (expr2) {
4    statements;
5    ...
6} else {
7    statements;
8}

In the above syntax, if expr1 evaluates to false, PHP will check expr2 and so on in sequence. If any of the subsequent expressions evaluates to true, the statement(s) in the corresponding block will be executed. If none of the expressions evaluates to true, the statements in the else block will be executed.

You may have as many elseif blocks as you like. But as the number of elseif blocks increases, it is advisable to consider the switch statement which is the subject of the next topic.

Example

The if...elseif...else statement.

 1<?php
 2    $salary = 8000;
 3    $KPI = 62;
 4    if ($KPI > 80)
 5    {
 6        $bonus = 1300;
 7        $salary += $bonus;
 8    }
 9    elseif ($KPI > 70)
10    {
11        $bonus = 1000;
12        $salary += $bonus;
13    }
14    elseif ($KPI > 60)
15    {
16        $bonus = 500;
17        $salary += $bonus;
18    }
19    else
20    {
21        $bonus = 0;
22        $salary += $bonus;
23    }
24    echo "Your bonus is $bonus. <br>" ;
25    echo "Your updated salary is $salary." ;
26?>
Your bonus is 500.
Your updated salary is 8500.

For the above example, we have the follow conditions:

  • If $KPI > 80, then $bonus = 1300.
  • If 70 < $KPI <= 80, then $bonus = 1000.
  • If 60 < $KPI <= 70, then $bonus = 500.
  • If $KPI <= 60, then $bonus = 0.
Caution

The order in which we place the conditions is important.

For example, if $KPI=92 and we had placed the condition $KPI > 70 as the first condition, the corresponding block will be executed, giving the erroneous result that $bonus = 1000.

To preclude this, we can often write down the complete ranges.

Example

The if...elseif...else statement.

 1<?php
 2    $salary = 8000;
 3    $KPI = 62;
 4    if ($KPI > 80)
 5    {
 6        $bonus = 1300;
 7        $salary += $bonus;
 8    }
 9    elseif ($KPI > 70 && $KPI <= 80)
10    {
11        $bonus = 1000;
12        $salary += $bonus;
13    }
14    elseif ($KPI > 60 && $KPI <= 70)
15    {
16        $bonus = 500;
17        $salary += $bonus;
18    }
19    else
20    {
21        $bonus = 0;
22        $salary += $bonus;
23    }
24    echo "Your bonus is $bonus. <br>" ;
25    echo "Your updated salary is $salary." ;
26?>
Your bonus is 500.
Your updated salary is 8500.