Reputation:
Couldn't the string representation of FALSE be "0"
, just like TRUE is juggled to "1"
?
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 values.
What do they mean by "allows conversion back and forth between boolean and string values".
So TRUE
does not allow that ?
CAn you explain that and give some examples please ?
NOTE: I Know there are other related questions, but none of them provided what i'm precisely looking for!! So don't bother copying a link!!
Upvotes: 0
Views: 74
Reputation:
Examine the following code:
var_dump("0" == false.""); // bool(false)
var_dump("" == false.""); // bool(true)
If false would be represented as "0"
, the given comparison would be false
as "0" != ""
.
This is the reason why false
is represented as ""
instead of "0"
, providing back conversion.
Upvotes: 2
Reputation: 9562
The answer to this question is more or less opinion based. And here is my interpretation:
Historically PHP uses FALSE as an output value in many scenarios in case of unsuccessful operation. For instance substr()
returns string or FALSE. Therefore, in my opinion, PHP optimized FALSE conversion for output purposes:
$name = 'JOHN';
$theSixthLetter = substr($name, 6, 1); // false
echo 'The sixth letter of the name is: ' . $theSixthLetter; // empty string
Upvotes: 0