Broad Network


PHP Conditional Statements

Basics of PHP with Security Considerations - Part 4

Foreword: In this part of the series, I explain how a group of statements can be executed based on a condition.

By: Chrysanthus Date Published: 30 Aug 2018

Introduction

This is part 4 of my series, Basics of PHP with Security Considerations. In this part of the series, I explain 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". You should have read the previous parts of the series before reaching here, as this is the continuation.

The if Construct
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. I present an example. Consider the following code:

<?php

    $hisVar = 20;

    if ($hisVar == 20)
        {
            echo "I am studying PHP, with Security Considerations.";
        }

?>

In the code, you have the statement that assigns the value 20 to the variable, $hisVar. Then you have the “if” construct. The if-construct begins with the reserved word, “if” and ends with the curly brace, }. Here, there is no semicolon after, }. What goes inside the parentheses is the condition. The statements to be executed are in the braces (curly brackets). The if-construct is not an ordinary statement, so it does not end with a semicolon. The whole if-construct (with the pair of parentheses and the pair of curly braces) can be called the if-statement.

If the condition is correct, PHP will replace it with, true, internally; you do not see that. 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-construct above can be read like this: if $hisVar equals 20 then display, "I am studying PHP, with Security Considerations.". Since we assigned the value 20 to $hisVar, the condition of the if-construct is true. So the statement in the curly braces is executed. Try the above code (replace all the content of the previous code in the temp.php file with it - in the home directory).

You can have more than one statement in the curly braces of the if-construct. If the condition is true, all the statements in the curly braces will be executed.

else
In the above code, the statement(s) in the curly braces is(are) executed if the condition is true. What 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 construct 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 this 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/else if
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” or "else if" reserved word as in the following code. Read and try it.

<?php

    $hisVar = 1000;

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

?>

A value of 1000 is assigned to hisVar. The if-elsif 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-elsif 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-elsif construct, the very first line must be the if-condition; the rest are elsif conditions. The elsif reserved word takes a condition, but the else reserved word never takes a condition.

Note that the reserved word is elseif and not elseif. There is no e between s and i.  

Always remember this: the if-elsif construct is used only for situations where only one of the conditions is satisfied (is true). At the limit, no condition may be satisfied (no condition may be true).

elsif is a contraction of “else if”.

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) something. 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 construct. The following code illustrates this:

use strict;

my $hisVar = 10000;

if ($hisVar == 10)
    {
        print 'Value is small';
    }
elsif ($hisVar == 100)
    {
        print 'Value is medium';
    }
elsif ($hisVar == 1000)
    {
        print 'Value is large';
    }
else
    {
        print '$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 again, 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
    }


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

<?php

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

?>

The basic syntax for the switch statement is:

switch ($variable)
    {
       case value:
           {  
               statements;
           }
       case value:
           {
               statements;
           }

       -  -  -
       default:
           {
               statements;
           }
    }

You begin with the switch expression. Then you have the big block. Inside the big block you have case blocks and the optional default block. The default block is the else situation. Note the use of colons (:). The curly brackets of the case blocks may be omitted. You use the switch statement instead of the if-elsif statement, when you want to compare the same variable with many different values.

Quotation Marks
If your value is a number in the case 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

Basics of PHP with Security Considerations
White Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links

Cousins

BACK NEXT

Comments