echo
is basically converting the boolean value to a string then printing it. The issue is that false
, when stringified, is the empty string ""
, while true
is a non-empty string '1'
.
Why is this odd behavior taking place? The creators of PHP want equality to remain transitive when converting types.
A common design question is: what strings are truthy (succeed when compared to true
) and what strings are falsy (succeed when compared to false
). Many languages consider the empty string ""
to by falsy and any non-empty string to be truthy.
From this link, you can see that:
(bool) "" == false
(bool) "1" == true
(bool) "0" == true // this is the important part
(bool) "true" == true
(bool) "false" == true // also of note
Its nice to have ((string) false) == false
and ((bool) ((string) false)) == false
.
Other languages, such as JavaScript, break transitivity for ==
. Many consider this to be a "bad part" of JavaScript.