DEV Community

Judith ☁ Oiku
Judith ☁ Oiku

Posted on

Difference between the assignment , equality & identity operator in PHP

There are several operators used in PHP to carry out programming operations.
= is the assignment operator, this assigns a value

e.g $x = 'hello world

== is the equality operator , it is used to test for equality

<?php
    $day = "Monday";
        if ($day == "Monday") echo "It is work day" 
?>
Enter fullscreen mode Exit fullscreen mode

Since $day = Monday and satisfies the condition, we have the output - It is work day

=== is the identity operator that is use to test if they are equal and of the same type

<?php
    $a = "1000";
    $b = 1000;
        if ($a === $b) 
            {
                echo "identical" ;
            } 
        else 
        {
            echo  "not identical"
        }
?>
Enter fullscreen mode Exit fullscreen mode

outputs not identical because $a is a string and $b is numerical, in other words they are of different type.

Top comments (0)