Foo Bar
Foo Bar

Reputation: 1892

Does PHP negation check with `!` coprrespond to `!=` or to `!==`?

In PHP, is

if(!$foo)

equivalent with

if($foo != true)

or with

if($foo !== true)

or is it even something completly different of both?

Upvotes: 0

Views: 3209

Answers (4)

RNK
RNK

Reputation: 5792

Note that,

== OR != compares the values of variables for equality, type casting as necessary. === OR !== checks if the two variables are of the same type AND have the same value.

This answer will give you better explanation of this concept: https://stackoverflow.com/a/80649/3067928

Upvotes: 4

Jay S.
Jay S.

Reputation: 1327

$a != $b

TRUE if $a is not equal to $b after type juggling.

$a !== $b

TRUE if $a is not equal to $b, or they are not of the same type.


See type juggling in PHP for more info on type juggling.


Sources : php.net

Upvotes: -1

Jelle Keizer
Jelle Keizer

Reputation: 721

Its not the same

!= is No equal (Returns true if  is not equal)
!== is Not identical  (Returns true if  is not equal , or they are not of the same type)

Upvotes: -1

Alexis Peters
Alexis Peters

Reputation: 1631

if(!$foo)

is the equivalent to

if($foo != true)

so

$foo = null;
if(!$foo){
 echo "asd";
}

will ouptut "asd"

Upvotes: 3

Related Questions