What is the PHP best practice for using functions that return true or false?

前端 未结 5 718
面向向阳花
面向向阳花 2021-01-13 02:40

After playing with PHP, I discovered that true is returned as 1 and false as null.

echo (5 == 5) // displays 1
echo (5 == 4) // displays nothing
5条回答
  •  梦谈多话
    2021-01-13 03:33

    After playing with PHP, I discovered that true is returned as 1 and false as null.

    No.. true and false are returned as boolean true and false. When you echo output it must be cast to a string for display. As per the manual:

    A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string). This allows conversion back and forth between boolean and string value.

    As for the rest: that's fine, yes, yes, when you want exact type matches, to avoid type juggling in comparisons, e.g. "1" == true is true but "1" === true is false.

提交回复
热议问题