Broad Network


Logical Operators in PHP

PHP Operators with Security Considerations - Part 4

Foreword: In this part of the series, I talk about logical operators in PHP.

By: Chrysanthus Date Published: 17 Oct 2018

Introduction

This is part 4 of my series, PHP Operators with Security Considerations. Logical operators are the operators for the Boolean AND, Boolean OR and Boolean NOT. In this part of the series, I talk about logical operators in PHP. I use the if-condition to explain logical operators. If you are new to programming, then you should have read the previous parts of the series before reaching here - this is the continuation.

Single Condition Expression Example
Consider the following code:

    <?php

        //tall means 20
        $me = 20;

        if ($me == 20)
            {
                echo("I am tall");
            }

     ?>

In the condition, (parentheses of if) there is only one expression, which is, ($me == 20). If this expression results in true, the if-block will be executed. If it results in false, the if-block will not be executed.

The above if-construct is equivalent to

        if (true)
         {
                echo("I am tall");
         }

For the if-construct to be executed, you do not need the expression of the variable with its equality operator. The expression in the if-condition, no matter how complex, will always result in a single true or a single false.

In the following code, which you should test, the condition results in false.

    <?php

        //short means 10
        $me = 10;

        if ($me == 20)
            {
                echo("I am tall");
            }

     ?>


The if-block (curly braces) in the above code has not been executed, because the condition results in false, since the value of the variable, $me, is 10 for “short” and not 20 for “tall”. The above if-construct is equivalent to:

if (false)
    {
        echo("I am tall");
    }

An if-block can only be executed if the condition is true. In this case it is not executed, because the condition is false.

More than One Expression in Condition
You can have more than one expression in a condition. In this part of the series, I consider a maximum of two expressions in a condition. Each of the expressions results in true or false. The expressions are combined with the AND, OR or NOT operators. The AND operator is typed as, &&. The OR operator is typed as, || . The NOT Operator is typed as ! . && and || , and ! are called logical operators. There are variations to these (see below). With logical operators, the rules (truth tables) for AND, OR and NOT can be written as:

AND
(false) && (false) = false
(false) && (true) = false
(true) && (false) = false
(true) && (true) = true

OR
(false) || (false) = false
(false) || (true) = true
(true) || (false) = true
(true) || (true) = true

NOT
!(false) = true
!(true) = false

Double-Expression Examples
The if-block will not be executed in the following code because the condition results in false:

    <?php

        if ((false)&&(true))
         {
                echo("We are tall");
         }

     ?>

A practical example for the above code is:

    <?php

        //Tall means 20 and short means 10
        $you = 20;
        $me = 20;

        if (($you == 10)&&($me == 20))
            {
                 echo("We are tall");
             }

     ?>

20 is assigned to the variable, $you, and also to the variable, $me. The first expression in the condition results in false and the second one results in true. (false)&&(true) gives false as the effective Boolean value for the condition. So the block is not executed.

The if-block will be executed in the following code:

    <?php

        if ((false)||(true))
         {
                echo("We are tall");
         }

     ?>

A practical example for the above code is:

    <?php

        //Tall means 20 and short means 10
        $you = 20;
        $me = 20;

        if (($you == 10)||($me == 20))
             {
                echo( "We are tall");
             }

     ?>

Try the above code. The first expression results in false; the second one results in true. The effective condition is true, since (false)||(true) gives true.

NOT Examples
The if-block will be executed in the following code:

    <?php

        if (!(false))
            {
                echo("I am tall");
            }

     ?>

The if-block is executed, if the condition is true. !(false) gives true.

A practical example for the above code is:

    <?php

        //Let tall mean 20 and short mean 10
        $me = 20;

        if (!($me == 10))
            {
                echo("I am tall");
            }

     ?>

Xor Operator
The EXclusive OR operator is like the OR operator, except that

    (true) Xor (true) = false

The truth table for Xor is:

XOR
(false) Xor (false) = false
(false) Xor (true) = true
(true) Xor (false) = true
(true) Xor (true) = false

Try the following code:

    <?php

        if ((true)Xor(true))
            {
                echo("It should not work here.");
            }
        else
            {
                echo("It should work here.");
            }

     ?>

and and Or Operators
The and operator is the same as the && operator. The difference is precedence - see later.
The or operator is the same as the || operator. The difference is precedence - see later.

Time to take a break. We 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