Although I consider myself a fairly seasoned programmer, I am actually relatively new to PHP. I have never had any formal training in PHP nor have I even read a book about it. Most of what I have learned is strictly from studying other code and good old-fashioned trial and error.

In over 15 years of programming experience, last week was the first time I saw the triple equals operator “===”. Let’s start with the single equals operator and build our way to the triple.

Single Equals
A single equal sign in PHP is an assignment operator. It assigns the value of the right side to the variable on the left side. For example:

$name = “jon”;
echo $name; //output is “jon”

Double Equals
Two equal signs in PHP is a comparison operator used to check if the value on the left is the same as the value on the right. This is what you normally use in If statements. For example:

$a = 5;
$b = 4+1;
// then $a == $b is true

Triple Equals
Three equal signs in PHP is also a comparison operator but looks not only at the value but also looks at the type of the variables. For example:

$age1 = “5″;
$age2 = 5;
// then $age1 == $age2 is true
// and $age1 === $age2 is false because the variables are of different types

I don’t know of any other language that uses the triple equals operator (it was only introduced in PHP4 after all) but many other languages use “=” and “==” in the same way as PHP.

Popularity: 8% [?]

Tags: , , , ,
14 Responses to “The Triple Equals in PHP”
  1. Luke says:

    I don’t think any other language out there uses the triple equals. :P But then again, in most duck typed languages this type of comparison would be a two step process. It’s nothing more than a syntactic sugar.

    This is a really good reference if you get confused by PHP’s multiple comparison operators and functions:

    http://www.blueshoes.org/en/developer/php_cheat_sheet/

  2. Damien says:

    I’m learning pHp so this was a good piece of data. Thanks.

  3. Cynthia Blue says:

    I love PHP. I haven’t worked in it very much, I’m not an expert, but I do enjoy it. Thanks for the triple equals info!

  4. [...] an article that’s right up my alley. Jon at jon lee dot see eh (obviously Canadian) wrote an article on the triple equals in PHP. I finally understand what the [...]

  5. maurizio says:

    I started to use === when I had to improve my script that uses strpos.

    $result = strpos(”abc”,”d”);
    doesn’t find “d” in “abc”, so it return false

    if ($result === false)
    echo “blah”;

    You can’t use == here, because false is 0, so if you look for “a” on the previous string, the result is 0 and the if statement (if $result == false) is true.

    I hope you understand me. :-)

  6. Jon Lee says:

    That’s a very good example… I would shorten it to something like this:
    if (strpos(”abc”,”d”) === false)
    echo “blah”;

    Assuming you don’t need $result somewhere else later… which you probably do, nevermind haha.

  7. maurizio says:

    :mrgreen:
    Well.. I can see at least an example where I don’t need the result (for example my Alexa script).. but I imagine 1000s example where I need it
    :mrgreen: :mrgreen:

  8. rimbaud says:

    Thanks for the clear explanation :-)

  9. Javascript also uses this syntax.

  10. Linda says:

    PHP and Javascript are not strict on variable types.

    If you follow a programming practice – always to ensure that a comparison is between two variables of the same type, you really do not have to use the === sign at all.

    http://www.gbandtb.com/

  11. hazed says:

    Well explained. I always wandered the difference between == and === myself and it can be so hard to search Google for the answer (the equal sign isn’t searchable).

    I’ll be using this from now on to ensure that all comparisons are the same variable type.

    Thanks.

  12. chris says:

    if(!$result) { …. }

    ;)

  13. Eric says:

    PHP does have some unique features and syntax. Another one of PHP’s unique features I think is interesting is variable variables, where the name of a variable can be variable:

    $a = “name”;
    $$a = “Bob”;

    In this example, the name of the variable $$a is “name”.

  14.  
Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>