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

前端 未结 3 1443
刺人心
刺人心 2020-12-11 17:30

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

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

相关标签:
3条回答
  • 2020-12-11 17:48

    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.

    0 讨论(0)
  • 2020-12-11 17:49

    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.

    0 讨论(0)
  • 2020-12-11 17:56

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

    $value = false;
    

    instead of

    $value === false;
    
    0 讨论(0)
提交回复
热议问题