snack_overflow
snack_overflow

Reputation: 546

PHP - What is the difference between '!== false' and ' == true'?

Sometimes I see people write conditional statements like this:

if($var !== false) {...}

Instead of like this:

if($var == true) {...}

These are the same, right?

I see the former used much more frequently and am wondering if there is a reason behind this, or if it is just personal preference.

I appreciate that this might be opinion based, but I am curious to see if there is a legitimate reason behind this.

Upvotes: 0

Views: 363

Answers (4)

Salman Arshad
Salman Arshad

Reputation: 272146

$var !== false could be used for something other than readability or personal preference. Various PHP functions are expected to return false in case of error and they might as well return a falsy value on success. Take strpos for example:

Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.

Returns FALSE if the needle was not found.

This means the function can return an integer, even 0 if the needle was found at the beginning, and false if needle was not found. You have to use !== false to check if the expression is false, not falsy.

Upvotes: 3

Neethu
Neethu

Reputation: 294

When you use ==

First, it typecast the variable and compare are those values same. You can see in the following example

var_dump(0==FALSE);  // ( 0 == ( int ) false  )  bool(true) 
var_dump(0=='anystring'); //  ( 0 == ( int ) 'anystring' ) bool(true)  

When you use ===

It compares the value and types too

So it would be something like

var_dump(  gettype( 0  )  == gettype( false  ) && 0 == false  )

This is faster in case if your type check fails Since it has not to typecast the value for further 'value check' if type check fails.

Upvotes: -1

DanSingerman
DanSingerman

Reputation: 36512

This:

 if($var !== false) {...}

will only evaluate to false if $var is exactly false. It will not evaluate to false if $var is any other value, whether 'false-y' or not.

This:

if($var == true) {...}

will evaluate to false for any 'false-y' value. e.g. 0, '0'.

In addition this:

if($var === true) {...}

will evaluate to true only if $var is exactly set to true, not other 'truthy-y' values.

So you are correct that they are the same if you know $var is exclusively one of either true or false, but they behave differently for other values.

Upvotes: 4

treyBake
treyBake

Reputation: 6560

They're not the same.

!== is a strict comparison that compares value and type. $var has to equal (bool) false. This means if a string of 'false' was returned it would fail.

== is a loose comparison that just checks the value. This means $var can equal '(string) string' and be true. When checking a var like this:

if ($var == true) {

}

you check if $var has anything in it/defined. As long as something is a against it (and doesn't equal (bool) false) it will pass the conditional. This means '(string) false' would pass that conditional.

Worth nothing some functions (like strpos) return (bool) false so doing the first one (IMO) is better for those sort of functions.

Upvotes: 2

Related Questions