PHP If Statement Tutorial

The PHP if statement is the same as the if statement in most if not all other programming and scripting languages. If statements allows the computer to make decisions based on the values of variables. This makes our applications and scripts smarter since they can now take action based on the input that is given.

If statements are also known as conditional statements. This simply means that they are able to take action based on various conditions.

The PHP if statement tutorial below takes a look at how to write if statements in PHP properly.

What does the PHP if statement do?

The PHP if statement tests if a condition is true and then takes a particular action based on that result. For example, in web programming you could use it to test if a user was logged in, and take action by showing them some private content or something of the sort.

PHP If Statement Syntax

The syntax of the if statement in PHP is as follows:

if (condition)
{
    // do a particular action only if TRUE
}

Condition, as used above, is a statement or statements which will evaluate to either TRUE or FALSE. That is, it could be a comparison, a check if a variable is set, or any other test that returns a boolean result. For comparisons, see the comparison operators section of our PHP operators tutorial.

PHP If Statement Example

Take a look at the code below:

<?php
 
    $variable = 5;
 
    if ($variable == 5)
    {
        echo 'Equal to 5';
    }
 
    if ($variable == 6)
    {
        echo 'Equal to 6';
    }
 
    echo '<br />Do this either way..';
?>

This code would give the following as the second echo would not be executed:

Equal to 5
Do this either way..

The second if statement was false so the code within the curly braces was not executed.

It is important to note that if you are only executing a single statement based on the outcome of the if, you can omit the curly braces. The following example would work in exactly the same way as the one above:

<?php
 
    $variable = 5;
 
    if ($variable == 5)
        echo 'Equal to 5';
 
    if ($variable == 6)
        echo 'Equal to 6';
 
    echo 'Do this either way..';
?>

PHP allows us to use an if-else statement when programming. The PHP if-else statement does everything the PHP if statement does except that it allows the programmer to take action if a condition is not met.

That is, we can use the if-else statement to perform an operation if a condition is met as well as if the condition is not met.

PHP If-Else Syntax

The syntax for the If-Else statement in PHP is as follows:

if (condition)
{
    // code to execute if true
}
else
{
    // code to execute if false
}

That is, we first check a condition and perform some instructions if that condition is met. If the condition is not met, we perform a different set of tasks.

PHP If-Else Statement Example

Using the if-else statement in PHP allows your scripts to become smarter. What this means is that your scripts are now able to take the relevant action based on the input it receives. Take the following for example:

<?php
 
    $age = 20;
 
    if ($age >= 21)
    {
        echo 'You are allowed to drink alcohol';
    }
    else
    {
        echo 'You are NOT allowed to drink alcohol';
    }
?>

The result of this script would be:

You are NOT allowed to drink alcohol

Immediately you should begin to see the power of the if-else statement. The if-else statement is probably the most popular construct used in any programming language.

As always, if you are only executing a single statement, you may omit the curly braces. Omitting the curly braces will make only the single statement that comes next a part of the clause. The example below will demonstrate:

<?php
 
    $age = 21;
 
    if ($age >= 21)
        echo 'You are allowed to drink alcohol';
    else
        echo 'You are NOT allowed to drink alcohol';
        echo '<br />Whitespace does not make a difference';
?>

The result of this script would be:

You are allowed to drink alcohol
Whitespace does not make a difference

The final echo gets executed as well since it is not a part of the else clause. This is important to note. Indenting an instruction does not make a difference. It only aids readability. If we wanted it to behave as a part of the else, we need to put it inside curly braces.

As we just saw, the PHP if else statement is useful for taking one of 2 actions; if a condition is met or if it is not met. If we had 3 or more possibilities and we wanted to do 3 or more different things based on the input, we would not be able to use the regular PHP if else construct. We would need a new construct. We cover this in our PHP elseif statement tutorial.

Link to this Page

Thank Tutorial Arena for This Tutorial.
Show your appreciation with a +1...