php

PHP If..Else Statements

The conditional statement is used to execute the block of statements based on the particular condition. The ‘If..else’ statement is one type of condition statement that works with the particular condition(s). Different types of ‘if..else’ statements can be used in PHP for implementing conditional statements. These are mentioned below.

A. Simple ‘if’ statement

Syntax:

if (condition(s)) {
    Statement(s)
}

The above if statement is used to implement the conditional statement for true value only.

B. ‘If..else’ statement

Syntax:

if (condition(s)) {
    Statement(s)
}
else {
    Statement(s)
}

The above if statement is used to implement the conditional statement for both true and false values.

C. ‘If..elseif..else’ statement

Syntax:

if (condition(s)) {
    Statement(s)
}
elseif (condition(s)) {
    Statement(s)
}
elseif (condition(s)) {
    Statement(s)
}
. . .
else {
    Statement(s)
}

The above if statement is used to implement the conditional statement for multiple if statements. If the first if condition returns false then it will check the second if condition and so on. The statement of the else part will be executed all if the statements return a false value.

D. Nested ‘if..else’ statement

Syntax:

if (condition(s)) {
If (condition(s)) {
        Statement(s)
}
else {
        Statement(s)
}
}
else {
    Statement(s)
}

The above if statement is used when the execution of one if statement depends on another if statement.

Pre-requisite

The scripts used in the examples of this tutorial are written based on the PHP 8 version. Do the following task before executing the examples of this script.

  1. Install apache2 and PHP 8.
  2. Set execute permission for all files and folders under /var/www/html folder where all PHP files will be stored.

Different Uses of If..Else Statement

The different uses of the ‘if..else’ statements mentioned above have been explained in this part of the tutorial by using examples.

Example-1: Use of Simple ‘If’ Statements

Create a PHP file with the following script to read a URL query parameter and print a message if the query parameter value matches with a particular value. Here, one if statement is used to check the query parameter is set or not, and another if statement is used to compare the parameter value with a string value.

<?php

//Check the value of name has given in the URL or not
if(isset($_GET['name']))
{
    $name = $_GET['name'];
    //Check the provided name is selected or not
    if (strtolower($name) == "jafariqbal")
        echo '<h2>You are selected.</h2>';
}

?>

Output:

The following output will appear after executing the above script with the query parameter. Here, the filename is if1.php that is stored inside /var/www/html/code folder. The query parameter name is “name” and the value is “Jafar Iqbal”.

http://localhost/code/if1.php?name=Jafar Iqbal

Example-2: Use of If..Else Statement

Create a PHP file with the following script to read a URL query parameter and print a message if the query parameter value matches with a particular value and print another message if the query parameter does not match. Here, one if statement is used to check the query parameter is set or not, and another if statement is used to compare the parameter value with a string value.

<?php

//Check the value of name has given in the URL or not
if(isset($_GET['name']))
{
    $name = $_GET['name'];
    //Check the provided name is selected or not
    if (strtolower($name) == "jafariqbal")
        echo '<h2>You are selected.</h2>';
    else
        echo "<h2>You are not selected.</h2>";
}
else
    echo "<h2>No name value has given.</h2>"

?>

Output:

The following output will appear after executing the above script without any query parameter. Here, the filename is if2.php that is stored inside /var/www/html/code folder.

http://localhost/code/if2.php

The following output will appear after executing the above script with a valid query parameter name. Here, the query parameter name is “name” and the value is “Jafar Iqbal”.

http://localhost/code/if2.php?name=Jafar Iqbal

The following output will appear after executing the above script with an invalid query parameter name. Here, the query parameter name is “name” and the value is “Jafar”.

http://localhost/code/if2.php?name=Jafar

Example-3: Use of ‘If..Elseif..Else’ Statement

Create a PHP file with the following script to read a URL query parameter and print a message based on if the condition returns true by comparing the value of the query parameter with a string value. If all if conditions return false then the message of the else part will be printed.

<?php

//Check the value of the name has given in the URL or not
if(isset($_GET['name']))
{
    $name = strtolower($_GET['name']);
    //Check the provided name is selected in which group
    if ($name == "jafariqbal")
        echo "<h2>You are in group-1.</h2>";
    elseif ($name == 'janifer')
        echo "<h2>You are in group-2.</h2>";
    elseif ($name == 'jony')
        echo "<h2>You are in group-3.</h2>";
    else
        echo "<h2>You are not selected.</h2>";
}
else
    //Print message if no value will be provided in the URL
    echo "<h2>No name value has given.</h2>"


?>

Output:

The following output will appear after executing the above script with a valid query parameter name. Here, the filename is if3.php that is stored inside /var/www/html/code folder. The query parameter name is “name” and the value is “janifer”.

http://localhost/code/if3.php?name=janifer

The following output will appear after executing the above script with a valid query parameter name. Here, the query parameter name is “name” and the value is “sakib”.

http://localhost/code/if3.php?name=sakib

Example-4: Use of Nested ‘If..Else’ Statement

Create a PHP file with the following script to print messages by comparing two string values with the nested if..else statements. Here, the outer if statement will compare the value of the department variable, and if it returns true then the inner if statement will compare the value of the post variable. The output will be generated based on the return values of both if statements.

<?php

 

    //Define two string values
    $department = 'HR';  
    $post = "Manager";  

    echo "<br />";

    //Check the first outer if condition
    if ($department == "Sales")  
    {  
        //Check the inner if condition
        if ($post == "Assistant Manager") {  
            echo "Total assistant managers are of <b>$department</b> department 25.";  
        }  
        if ($post == "Manager") {  
            echo "Total managers of <b>$department</b> department are 10.";  
        }
    }  
    //Check the second outer if condition
    elseif ($department == "HR")
    {  
         //Check the inner if condition
        if ($post == "Assistant Manager") {  
            echo "Total assistant managers of <b>$department</b> department are 20.";  
        }  
        if ($post == "Manager") {  
            echo "Total managers of <b>$department</b> department are 5.";  
        }
    }  

?>

Output:

The following output will appear after executing the above script. Here, the filename is if4.php that is stored inside /var/www/html/code folder.

http://localhost/code/if4.php

Conclusion

Four different uses of ‘if..else‘ statements have been explained in this tutorial to help the PHP coders to understand the use of the conditional statement in PHP properly. The alternative of ‘if..else’ statement is a switch statement but it has some limitations.

Video Tutorial

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.