-1

I often see if's like:

if (null === $var)  

I wanted to know wether there is any technical advantage by using it?

I find it semantically wrong and I don't like it. Prefer

if ($var === null)

because I ask for the condition of a variable, not for a variable having a condition.

Daniel W.
  • 31,164
  • 13
  • 93
  • 151
  • i use $var === null .. both are correct, use the style you prefer – donald123 Jan 05 '15 at 09:05
  • If this is opinion based, well, would answer my question (no, no technical advantage). But why downvote... – Daniel W. Jan 05 '15 at 09:06
  • I think it stems from the practice of using non-lvalues on the left side of a == operator. If you accidentally use =, then most languages will throw an error about an invalid assignment, but you might miss it if the terms were switched. – Paul Dixon Jan 05 '15 at 09:07
  • I admit this is not exactly easy to google for, but it has been asked countless times. Just follow the trail of dupe closes... – deceze Jan 05 '15 at 09:08

1 Answers1

1

This type of conditions is sometimes referred to as yoda conditions. Their main advantage is that they avoid accidental assignment:

if ($foo = 'bar')
    echo 'Evaluates to true, and reassigns $foo silently';
if ('bar' = $foo)//error

Other than that, it's a matter of personal preference

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149