#### Archived

This topic is now archived and is closed to further replies.

# is there a difference between if(\$var == false) and if(false == \$var)

## Recommended Posts

hi there,

is there a difference between if(\$var == false) and if(false == \$var)

thankyou!

##### Share on other sites

No there isn't. It's just comparing. It's the same as asking if this apple is the same color as this orange; or you might as well ask if this orange is the same color as this apple.

But there is difference in \$var == false and \$var === false

\$var = 0;

\$var == false // TRUE

\$var === false // FALSE

=== will also compare the variable type. It checks if it's a bool, string or integer for example.

\$var = "1";

\$var == 1 // TRUE

\$var === 1 // FALSE

##### Share on other sites

Which is the same as the logical operator ! (NOT)

```\$var = false;

if (!\$var)
{
echo "var is equal to false";
} else {
echo "var is equal to true";

}
```

Returns var is equal to false.

##### Share on other sites

Which is the same as the logical operator ! (NOT)

```\$var = false;

if (!\$var)
{
echo "var is equal to false";
} else {
echo "var is equal to true";

}
```

Returns var is equal to false.

It would be

if (\$var != true)

##### Share on other sites

Which is the same as the logical operator ! (NOT)

```\$var = false;

if (!\$var)
{
echo "var is equal to false";
} else {
echo "var is equal to true";

}
```

Returns var is equal to false.

It would be

if (\$var != true)

if(!\$var) and if(\$var != true) or if(\$var == false) all produce the same result -> "var is equal to false" (not entirely correct it could be aswell: null, '', array(), 0).

Junior programmers are often told to write if(false == \$var) to make sure they'll never write if(false = \$var) or if(1 = \$var) as this will produce a fatal error, you can't redefine false nor 1.

In case of boolean variables (depending on your companies coding guidelines) you can shorten if(\$var == false) to if(!\$var) as you actually say the same thing: execute when \$var = false

##### Share on other sites

I'm sure i read somewhere that where comparing like:

if ( \$var === TRUE )

It's quicker for php to process:

if ( TRUE === \$var )

An even faster way would be:

if ( !\$var == TRUE )

And EVEN faster:

if ( !empty(\$var) )

×

• #### Activity

• Chat
×
• Create New...