Home » Php » php – What is a reason of placing boolean or null before comparison operator?

php – What is a reason of placing boolean or null before comparison operator?

Posted by: admin July 12, 2020 Leave a comment

Questions:

In PHP, what is an underlying reason of placing either boolean or null before identical comparison operator?

false === $value;   
null === $value;

It seems to me that it is same as saying

$value === false;

Is it just a personal preference or there is a concrete reason why people do this?

How to&Answers:

It’s a convention to avoid the mistake of accidentally assigning a variable.

$value = false;

instead of

$value === false;

Answer:

This is sometimes referred to as Yoda-conditions, there’s a fun list of all such constructs and their unofficial names.

No there’s no real difference between $var === false or false === $var, some people claim it’s easier to see what is being checked for if the bool is the left operand, other hate it… In short: personal preference is what it is.

Answer:

It’s supposed to be quicker, but I can’t lay a hand on an authority saying this with a simple Google search. See:

http://forums.phpfreaks.com/topic/222939-is-there-a-difference-between-ifvar-false-and-iffalse-var/

for one opinion.