Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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?

like image 848
Ken Avatar asked Oct 17 '12 17:10

Ken


Video Answer


2 Answers

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

$value = false;

instead of

$value === false;
like image 171
nalply Avatar answered Nov 15 '22 00:11

nalply


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.

like image 31
Elias Van Ootegem Avatar answered Nov 14 '22 23:11

Elias Van Ootegem