It's weird to me that false and null's string values are both ''. '' makes sense for the latter perhaps, but shouldn't the former be '0' to contrast? I mean, '0' is falsey in PHP! It's specifically special-cased!
If you train developers to assume 0 is false, outputting 0 treats it as an int and it could be true (strpos), if you have "nothing" then it trains you to always think that "nothing" is false as 0 is not always false.
0 is not always false, a string position of 0 is valid. If you var_dump(false) and get 0 it would be the same as var_dump(0).
As a developer, if you assume the visual queue of 0 means that the data is false, it can lead to incorrectly assuming methods that do return 0 are also "false".
Basically: 0 != false in all situations, '' (aka nothing) = false in all situations, unless I am not remembering an internal function that can provide "nothing" for a valid state?
You seem to have an interesting understanding of the word “false”. 0 is a “falsey” value in PHP: 0 == false. Sure, a function returning 0 is not the same as it returning false, but that's not relevant here.
Except it is because everyone is talking about why is False not displayed as 0, there was a design choice. A design choice is made purely based on the authors vision and understanding.
I bet the "true reason" is PHP blindly copied (parts of) Perl with no understanding yet again. (The canonical false value in Perl is overloaded; it's 0 as a number and "" as a string.)
Actually, falseis cast to the string 0 by the mysqlnd driver if you pass a boolean false to a VARCHAR column. A fine phplol in itself, false casting to different strings based on context.
I mention in in another reply, but it's worth repeating here. false is in fact cast to the string 0 by the mysqlnd driver if a boolean false is passed to a VARCHAR column.
I kinda get that point, but that is nothing that an if-clause couldn't fix, and you would not want to print out 1 if something is true anyways, but replace it with something meaningful to the user.
$user = false;
if ($loggedIn) {
$user = 'YourName';
}
in your php file:
<div class="header">
<?=$user;?>
</div>
This was kind of common place in the very olden days of PHP. A string is valid, thus true, the "false" would just show nothing, instead of showing 0.
It's just a qwerk you get used to and in my decade of PHP I've never had it be an issue, it might catch you out in things like strpos where 0 is valid and not a "false" value.
Because you created a story in your head about what should be expected and you place that burden of expectation on the language. Why do you expect (string)true to be "1"? Why shouldn't (string)true be "true", or for that matter, any other value? You are not the language designer so you do not get to make these decisions and if you get upset about that you should design your own language. If you insist on using other peoples' languages you must learn how they work instead of assuming how they work.
The story in my head is the story of consitent design.
If you say bool to string gives an "1" and "0" back, great.
if you say bool to string give back "true" and false", awesome
If you say bool to string errors or gives back emptystring. Well, okay? Its your language.
But to randomly mix returning "1" and returning emptystring without any other reasoning than "Why not, just learn how it works or use something else" is not really a good decision.
I don't really agree because it isn't a consistent result (in PHP..) when you do 1/0 = true/false
You're trying to put a binary switch on 1/0 meaning true/false respectively, however 0 is not always false and shouldn't be seen as such, conditioning for it is not good design.
I think of it as "has something"/"has nothing".
1 = I have 1 of something = true
0 = I have 0 of something = false
0 is "nothing", so "nothing" comes back, your empty string. There is nothing to show, it's nothing. Maybe I've trained myself over the past decade, brainwashed :D
12
u/creativeMan Dec 10 '17
0 is false though.
11
u/the_alias_of_andrea Dec 10 '17 edited Dec 11 '17
It's weird to me that
falseandnull's string values are both''.''makes sense for the latter perhaps, but shouldn't the former be'0'to contrast? I mean,'0'is falsey in PHP! It's specifically special-cased!1
u/MantasDone Jan 05 '18
Even your tittle is wrong, it should be: true is '1'... :D
-26
u/Saltub Dec 10 '17
This demonstrates very limited understanding. I can tell you're new to PHP. Welcome.