The 3 different equals

PhpComparisonOperators

Php Problem Overview


What is the difference between =, ==, and ===?

I think using one equal sign is to declare a variable while two equal signs are for a comparison condition and lastly three equal signs are for comparing values of declared variables.

Php Solutions


Solution 1 - Php

You have = the assignment operator, == the 'equal' comparison operator and === the 'identical' comparison operator.

$a = $b     Assign      Sets $a to be equal to $b.
$a == $b    Equal       TRUE if $a is equal to $b.
$a === $b   Identical   TRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4)

For more info on the need for == and ===, and situations to use each, look at the docs.

Solution 2 - Php

  • = is the assignment operator
  • == is the comparison operator (checks if two variables have equal values)
  • === is the identical comparison operator (checks if two variables have equal values and are of the same type).

Solution 3 - Php

= assignment operator

== checks if two variables have the same value

=== checks if two variables have the same value AND if their types are the same

Solution 4 - Php

The = operator assigns the value to a variable $six = 6; value 6 is assigned to variable $six

== operator check if the value of both variables is equal and mostly used in conditions like if statements

$a = 2;
$b = 2;
if ($a == $b) { 
    echo both variables have the same value; 
}

=== operator similar to == (check if the value equals) and also check if both of same data type

$a = 2;
$b = "2";
if ($a === $b) {
    echo "both variable have same value and of same data type";
} else {
    echo 'both variable is either not equal or not of same data type';
}

// here $a is of type int whereas $b is of type string. So here the output

Solution 5 - Php

For advanced PHP users, knowing the difference between ==and === and asking themselves "is it faster to compare with == or with === when I'm sure that both the operands are the same type?"

The short and general answer is: There is no performance gain in using === in this cases, so you should probably use ==.

For the ones interested in benchmarking it themselves, you can use the following code I wrote ad-hoc and try different values for $a and $b:

<?php
    // CONFIGURATION
    $cycles = 1000000;
    $a = 'random string 1';
    $b = 'random string 2';

    // FUNCTIONS
    function compare_two_equals($a, $b) {
        if ($a == $b) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    function compare_three_equals($a, $b) {
        if ($a === $b) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    // EXECUTION
    $time = microtime(TRUE);
    for ($count_a = 0; $count_a < $cycles; $count_a++) {
        compare_two_equals($a, $b);
    }
    $time_two_a = microtime(TRUE) - $time;
    $time = microtime(TRUE);
    for ($count_a = 0; $count_a < $cycles; $count_a++) {
        compare_three_equals($a, $b);
    }
    $time_three_a = microtime(TRUE) - $time;
    $time = microtime(TRUE);
    for ($count_a = 0; $count_a < $cycles; $count_a++) {
        compare_two_equals($a, $b);
    }
    $time_two_b = microtime(TRUE) - $time;
    $time = microtime(TRUE);
    for ($count_a = 0; $count_a < $cycles; $count_a++) {
        compare_three_equals($a, $b);
    }
    $time_three_b = microtime(TRUE) - $time;
    $time = microtime(TRUE);

    // RESULTS PRINTING
    print "<br />\nCOMPARE == (FIRST TRY): " . number_format($time_two_a, 3) . " seconds";
    print "<br />\nCOMPARE == (SECOND TRY): " . number_format($time_two_b, 3) . " seconds";
    print "<br />\nCOMPARE === (FIRST TRY): " . number_format($time_three_a, 3) . " seconds";
    print "<br />\nCOMPARE === (SECOND TRY): " . number_format($time_three_b, 3) . " seconds";
?>

NOTE: The comparison is valid only when each "FIRST TRY" is very close to its "SECOND TRY". If they are significantly different, it means that the processor was busy doing something else while executing the comparisons and so the results are unreliable and the benchmark should be run again.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStrawberryView Question on Stackoverflow
Solution 1 - PhpgnarfView Answer on Stackoverflow
Solution 2 - PhpRich AdamsView Answer on Stackoverflow
Solution 3 - PhpSilvio DonniniView Answer on Stackoverflow
Solution 4 - PhpGideon BabuView Answer on Stackoverflow
Solution 5 - PhplucaferrarioView Answer on Stackoverflow