Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do !== and === mean in PHP? [duplicate]

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
like image 779
xRobot Avatar asked Nov 02 '10 17:11

xRobot


People also ask

What does == and === mean in php?

== Operator: This operator is used to check the given values are equal or not. If yes, it returns true, otherwise it returns false. Syntax: operand1 == operand2. === Operator: This operator is used to check the given values and its data type are equal or not. If yes, then it returns true, otherwise it returns false.

What is !== In php?

” and “==!” in PHP. !== Operator: It is called as non-identical operator. It returns true if operands are not equal, or they are not of the same type.

What does === means?

The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.

What is the difference between double equal and triple equal in php?

The operator == casts between two different types if they are different, while the === operator performs a 'typesafe comparison'. That means that it will only return true if both operands have the same type and the same value.


3 Answers

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

like image 146
Gabi Purcaru Avatar answered Nov 02 '22 18:11

Gabi Purcaru


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
like image 42
Sarfraz Avatar answered Nov 02 '22 19:11

Sarfraz


=== 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)
like image 29
Vincent Savard Avatar answered Nov 02 '22 18:11

Vincent Savard