The 3 different equals

48,215

Solution 1

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

  • = 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

= 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

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

Share:
48,215

Related videos on Youtube

Strawberry
Author by

Strawberry

I want to learn industry practices and apply them to my projects to make myself successful

Updated on June 04, 2020

Comments

  • Strawberry
    Strawberry almost 4 years

    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.

    • InfantPro'Aravind'
      InfantPro'Aravind' over 14 years
      check out the example ... I have posted it would clarify more things ..
    • lucaferrario
      lucaferrario almost 11 years
      For advanced PHP users, knowing the difference between == and === and asking themself "is it faster to compare with == or with === when I'm sure that both the operands are the same type?", please look at my benchmark script below.
    • Funk Forty Niner
      Funk Forty Niner almost 8 years
      @Strawberry I feel the question's tag should also include the javascript tag since it also applies to that language.
    • Funk Forty Niner
      Funk Forty Niner almost 8 years
      (Update): I have added the javascript tag in an edit, since there have been quite a few questions where people were using = in javascript. That doesn't only apply to PHP but other languages also.
    • JJJ
      JJJ about 6 years
      I went ahead and deleted the JavaScript tag: all answers here are only about PHP (except the latest which is very weak), there are differences between how the languages handle it, and there is already a similar de facto canonical JavaScript question (Which equals operator (== vs ===) should be used in JavaScript comparisons?).
  • Phil Perry
    Phil Perry over 10 years
    Also know about != and !== 'not' versions of the two comparison operators. Some languages use := as assignment, just to avoid this kind of confusion.
  • gnarf
    gnarf over 10 years
    Microbenchmarks like this are not very reliable. It is also highly unlikely that you'd ever want to worry about == or === as the cause of your performance problem. IMO: It's better to be strict (===) unless you explicitly want to be loose (==) about your comparisons. The number of strange edge cases i.e. "5 is not a number" == 5 can lead to wacky bugs. === never suffers from this problem.
  • stloc
    stloc over 8 years
    when I compare two identical datetime, I have false result, why ? example : sandbox.onlinephpfunctions.com/code/…
  • Andrea
    Andrea over 8 years
    @stloc, === with objects tells you if they are the same object, not if they have the same content.
  • lucaferrario
    lucaferrario over 8 years
    My test was to tell programmers that if they're choosing === for performance reasons, they're wrong. So programmers are free to choose === or == based on logical reasons but not on performance reasons: there are different cases to prefer the one or the other and performance must not be taken into account at all.
  • mvorisek
    mvorisek almost 6 years
    Introduced in PHP4, funny in Y2018 ;-)