What do !== and === mean in PHP?

Possible Duplicates:
How do the equality (== double equals) and identity (=== triple equals) comparison operators differ?
Reference - What does this symbol mean in PHP?
php not equal to != and !==

What are the !== and === operators in this code snippet?

if ( $a !== null ) // do something
if ( $b === $a ) // do something

They are strict type comparison operators. They not only check the value but also the type .

Consider a situation when you compare numbers or strings:

if (4 === 4) // same value and type
{
  // true
}

but

if (4 == "4") // same value and different type but == used
{
  // true
}

and

if (4 === "4") // same value but different type
{
  // false
}

This applies to objects as well as arrays.

So in above cases, you have to make sensible choice whether to use == or ===

It is good idea to use === when you are sure about the type as well

More Info:

  • http://php.net/manual/en/types.comparisons.php

  • They are identity equivalence operators.

    1 == 1
    1 == "1"
    1 === 1
    1 !== "1"
    true === true
    true !== "true"
    true == "true"
    

    All of these equate to true. Also check this link provided by @mbeckish


    === also checks for the type of the variable.

    For instance, "1" == 1 returns true but "1" === 1 returns false. It's particularly useful for fonctions that may return 0 or False (strpos for instance).

    This wouldn't work correctly because strpos returns 0 and 0 == false

    if (strpos('hello', 'hello world!'))
    

    This, however, would work :

    if (strpos('hello', 'hello world!') !== false)
    
    链接地址: http://www.djcxy.com/p/58600.html

    上一篇: 如何在php codeigniter中动态获取数据时隐藏td?

    下一篇: 什么做!==和===在PHP中意味着什么?