Broad Network


PHP Conditional Statements

Basics of PHP – Part 4

Forward: In this part of the series, we see how a group of statements can be executed based on a condition.

By: Chrysanthus Date Published: 28 Jul 2012

Introduction

This is part 4 of my series, Basics of PHP. In this part of the series, we see how a group of statements can be executed based on a condition. It is similar to what happens in a human language. For example, somebody can say, if a condition is true, do that and that and that.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

The if Statement
In PHP, there is a reserved word, which is “if”. The “if” must be in lowercase. This is used to check if a condition is true. If it is true, one or more statements are executed. Let us look at an example. Consider the following statements:

    <?php

        $hisVar = 20;

        if ($hisVar == 20)
            {
                echo "I am studying PHP";
            }

    ?>

The first statement assigns the value 20 to the variable, hisVar. Then you have the “if” statement. The if-statement begins with the reserved word, “if” and ends with the curly brace, }. What goes inside the parentheses is the condition. The statements to be executed are in the curly braces. If there is only one statement, you do not need the curly braces. If you have more than one statement, separate them with semicolons and put them within the curly braces, {}.

If the condition is correct, PHP will replace it with, true, internally; you do not see it. If it is wrong, PHP will replace it with, false, internally.

In the above code, 20 was assigned to, $hisVar. So, $hisVar equals 20. In the condition the equal sign is two assignment operators: one just next to the other. The if-statement above can be read like this: if $hisVar equals 20 then display, 'I am studying PHP'. Since we assigned the value 20 to $hisVar, the condition of the if-statement is true. So the statement in the curly braces is executed. Try the above code

else
In the above code, the statement(s) in the curly braces is(are) executed if the condition is true. What about, if it were false? It would be false if we never assigned 20 to $hisVar. If it were false, nothing will happen. That is, the statement(s) in the curly braces will not be executed. There is an else sub statement you can attach to the if-statement. The else part is similar in coding to the if part. However, its block (curly braces) is executed when the if’s condition is false. The else part does not have any condition. Try the following code:

    <?php

        $hisVar = 36;

        if ($hisVar == 20)
            {
                echo 'I am studying PHP';
            }
        else
            {
                echo 'I am doing something else';
            }

    ?>

In the above code, a value of 36 is assigned to $hisVar. In the if-condition, we test if $hisVar is equal to 20. So the condition returns false, and the statement(s) in the else block is (are) executed. Note how the else section has been typed. Also note that else is a reserved word.

elseif
You may have more than one test to make in a particular situation or for the same variable. In this case you include the “elseif” reserved word as in the following code. Try it.

    <?php

    $hisVar = 1000;

    if ($hisVar == 10)
        {
            echo 'Value is small';
        }
    elseif ($hisVar == 100)
        {
            echo 'Value is medium';
        }
    elseif ($hisVar == 1000)
        {
            echo 'Value is large';
        }

    ?>

A value of 1000 is assigned to hisVar. The if-elseif coding will test if $hisVar is 10; if it is (which it is not) the corresponding block will display 'Value is small'. The code will then test if $hisVar is 100; if it is (which it is not), the corresponding block will display, 'Value is medium'. The code will then test if $hisVar is 1000; if it is, the corresponding block will display, 'Value is large'. With the if-elseif coding only one of the blocks can be executed; that is, only one of the conditions can be true (the rest should be false).

In the if-elseif coding, the very first line must be the if-condition; the rest are elseif conditions. The elseif reserved word takes a condition, but the else reserved word never takes a condition.

Always remember this: the if-elseif coding is used only for situations where only one of the conditions is satisfied (is true).

Default Condition
What about the situation for an if-elseif coding where none of the conditions is true? For that situation you will need to report (inform the user) of something to that effect. This is an opportunity to give some default answer. You do this by simply adding the else (no condition) section at the end of the if-elseif coding. The following code illustrates this:

    <?php

    $hisVar = 10000;

    if ($hisVar == 10)
        {
            echo 'Value is small';
        }
    elseif ($hisVar == 100)
        {
            echo 'Value is medium';
        }
    elseif ($hisVar == 1000)
        {
            echo 'Value is large';
        }
    else
        {
            echo '$hisVar is very large';
        }

    ?>

Try the above code, if you have not already done so. At the start of the code, 10,000 is assigned to the variable. Note that when you are applying numbers with more than 3 digits, you do not use commas (you type 10000 and not 10,000). In the code, none of the conditions is satisfied, so the last block, which does not have any condition (which is the else part), is executed. Read through the code to appreciate this.

Complete Syntax for if-Statement
The complete syntax for the if-statement is:

if (condition)
    {
        statements
    }
elseif (condition)
    {
        statements
    }
elseif (condition)
    {
        statements
    }

            -  -  -

else
    {
        statements
    }

Note: if the “if” or “elseif” or “else” part has just one statement, then you do not need curly braces for the statement. You need curly braces if there is more than one statement.

The switch Statement
The previous code is replaced by the following. Read and try it.

    <?php

        $hisVar = 10000;

        switch ($hisVar)
            {
                case 10:
                echo 'Value is small';
                break;
                case 100:
                echo 'Value is medium';
                break;
                case 1000:
                echo 'Value is large';
                break;
                default:
                echo '$hisVar is very large';
        }

    ?>

The syntax for the switch statement is:

switch (expression)
    {
       case label :
           statements;
           break;
       case label :
           statements;
           break;
       -  -  -
       default :
           statements;
    }

With time you will know different possible expressions. An example is just a variable, as in the above case. label is the result of the expression. Each case ends with “break;”. The last situation does not have a label (corresponds to else). Note the use of colons and semicolons. If you have more than one statement, separate them with semicolons. Also note the use of the curly braces. You use the switch statement instead of the if-elseif statement, when you may want to compare the same variable (or expression) with many different values.

Quotation Marks
If your value is a number or a Boolean variable in the condition, you do not need to have it in quotes. However, if it is a string, you need to have it in quotes.

Let us stop here and continue in the next part of the series.

Chrys

Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message