Broad Network


PHP Comparison Operators with Security Consideration

PHP Operators with Security Considerations - Part 3

Foreword: In this part of the series I explain comparison operators in PHP.

By: Chrysanthus Date Published: 17 Oct 2018

Introduction

This is part 3 of my series, PHP Operators with Security Considerations. Equality operators are == and != (see explanation below). Identity operators are === and !== (see explanation below). Relational operators are <, >, <= and >= (see explanation below). These are all comparison operators in PHP. In this part of the series I explain comparison operators in PHP. You should have read the previous parts of the series before coming here as this is the continuation.

The Equal Operator
It is ==, typed as a double assignment operator. It is not = which is the assignment operator. The equal operator returns true if the operands on either side are equal, otherwise it returns false. You should have seen examples of its use before.

The Not-Equal Operator
The Not-Equal operator is the opposite of the Equal Operator. The Not-Equal operator is, != . It returns true if the operands are not equal, otherwise it returns false. Let us look at some examples:

Try the following code:

    <?php

        $myVar = 25;
        $hisVar = 30;
        if ($myVar != $hisVar)
            {
                echo("The values of the two variables are not equal.");
            }

     ?>

$myVar is 25, $hisVar is 30. The condition is read like this: If $myVar is not equal to $hisVar, then the if-block will be executed. Since the values of the variables are not equal, ($myVar != $myVar) results in true.

In the following code, the values of the two variables are equal, so the condition returns false and the if-block is not executed.

    <?php

        $myVar = 50;
        $hisVar = 50;
        if ($myVar != $hisVar)
            {
                echo("The values of the two variables are not equal.");
            }

     ?>

The Identity Operator

The Strict Equal Operator
The Strict Equal Operator is ===. It is similar to the Equal Operator, but here, it is not only the two values that have to be equal; the types of the two values should also be equal. 2.5 and "2.5" as operands return true for the Equal Operator, but they return false for the Strict Equal Operator. This is because 2.5 and "2.5" have the same value but they do not have the same type, since 2.5 is of data type, float and "2.5" is of data type, string. Try the following code:

    <?php

        $myVar = 2.5;
        $hisVar = "2.5";
        if ($myVar === $hisVar)
         {
                echo("The values of the two variables are identical.");
         }
        else
         {
                echo("The values of the two variables are not identical.");
         }

     ?>

This operator is also known as the identical (identity) operator.

The Strict Not-Equal Operator
The Strict Not-Equal Operator is !==. It is the opposite of the Strict Equal Operator. Here, if the two values are not equal or not of the same type, true is returned; if the two values are equal but not of the same type, true is returned; if the two values are not equal but of the same type, true is returned; otherwise, false is returned. !== is the opposite of === . Try the following:

    <?php

        $myVar = 2.5;
        $hisVar = "2.5";
        if ($myVar !== $hisVar)
         {
                echo("The values of the two variables are not identical.");
         }

     ?>

The Greater Than Operator
The Greater Than operator is, > . It returns true if the left operand is greater than the right operand. In the following example, the left operand is greater than the right operand. So the if-block is executed:

    <?php

        $var1 = 60;
        $var2 = 70;
        if ($var2 > $var1)
         {
                echo("The value of var2 is greater than the value of var1.");
         }

     ?>

Try the above code.

Greater Than Or Equal Operator
The Greater Than or Equal operator is, >= (it is the math greater than sign followed by the math equal sign). It returns true if the left operand is greater than or equal to the right operand.

The Less Than Operator
The Less Than Operator is < .It returns true if the left operand is less than the right operand.

The Less Than or Equal Operator
The Less than or Equal operator is, <= . It returns true if the left operand is less than or equal to the right operand.

Security Consideration
In PHP, a float number is hardly represented in the computer at the exact precision that it is typed. So, do not trust the result of the equal (== or !=) and identical operator (=== or !==) when the two operands are floats. With relational operators (<, >, <= and >=) do not trust the result when the two floats are close in value. How close, for you not to trust, is difficult to tell from the way PHP represents floats.

That is it for comparison operators. Let us take a break here. Rendezvous 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