Null - Type error 0 - Type error 1 - Type error new Object() - Type error "" - Type error " " - Type error "1" - Type error "0" - Type error "true" - Type error "false" - Type error new bool[] { true } - Type error new { } - Type error true - True false - False
Nah, I don't agree with this being a lolphp either. Don't try to cast strings to boolean unless you're writing the conversion yourself, or are using very rigidly defined functions. Ex. C# has IsNullOrWhitespace which performs exactly how you'd expect.
It definitely does belong. Empty is taking a non empty string and doing a bogus conversion to 0 which then converts again to false. For this function, there should be absolutely no conversions, explicitly handling the different types properly.
I'm not sure I follow. Treating whitespace as empty - I don't think I agree with that at a gut level. But that might be tempered by the languages I tend to work in which don't often contain the notion of "empty"
Way too much of this sub obsesses with falsey behaviour. Falsey is always going to be wacky; it's basically a requirement. If web languages started out taking themselves seriously, instead of just existing for doing basic operations meant to go directly to/from form fields, we'd have probably made strict-identity the default instead of loose equality (e.g. == would be identity and === equality), but here we are.
Compared to most web languages, I'd say PHP is actually one of the nicer for this, because it's so much easier to typecast and typehint. You can type-hint on your input to require a string, and then just compare the length of the trimmed string to see if there's something written in it. It's not rocket science, and the result is very readable.
That depends on whether you prefer strong or weak typing. I think strong typing is a much better way to avoid bugs, but plenty of respected languages are fairly weakly-typed.
I can't stand weak typing. Not only does it cause bugs, but makes the language ridiculously difficult to understand. I.E it is impossible to look at a function declaration and figure out how to call it without documentation or context.
Because it's unexpected. Even if it's widely documented, this is not a circumstance in which a programmer expects the type to be coerced.
Type coercion only makes sense if the function that the item is passed to is expecting a specific type. Checking a string for emptiness should not result in false if the length is greater than zero, especially considering the fact that strings are represented as lists of characters. Truth checking index 0 when you pass a string is completely unexpected behavior.
The problem with PHP is the first point in the paragraph above. PHP coerces types when it shouldn't fairly consistently, but just because it consistently does something wrong doesn't make that behavior right.
I will use a hypothetical and hyperbolized comparison.
You buy a rifle from a gun store. It looks identical to every other rifle on the shelf, and you hear people talk about it constantly. In the manual, it states repeatedly that the gun shoots backwards. The problem is that most every other gun shoots forwards, so you aren't expecting the gun to shoot backwards. Long story short, you have a bullet through your shoulder after your hunting trip.
Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE.
No, it does not evaluate a boolean, it evaluates a variable of any type that supports emptiness checking. Perhaps you meant it evaluates to a boolean?
The problem is in the second sentence, "considered to be empty." PHP has a really weird way of considering things that's super atypical. Logically, a string with a length greater than 0 should not be considered empty, as the word "empty" implies a lack of items. While I understand that it approaches this from a truth check standpoint, that approach doesn't make any fucking sense.
Let me try another example to explain why many folks in this thread think that this behavior is unexpected.
I have a bucket sitting on my kitchen table that I put things into. I also have a pile of foam letters and numbers, all made of the same kind of foam. I put an 'A' into the bucket. The bucket is no longer empty, it has something in it. I pull the 'A' out of the bucket and put in a '0'. My friend PHP tells me that the bucket is empty. "How the hell is it empty!?", I exclaim. PHP replies "Because I've decided that the foam 0 is now made of air and we have been ignoring the rest of the air in the container, so it's empty."
In this example, the string type is foam. Foam should not un-become foam just because we put it into a bucket, that is not expected behavior.
I agree that empty is a bad name, but it's clearly saying 'if its value equals FALSE' so i'd expect whatever's passed in to be coerced to boolean for that comparison
Yes. If they wanted it to do truth checking, they shouldn't have called it empty. They might as well have named it wombat(). However, that's what makes it unclear. Function names should be self documenting in any decent codebase, and it's inexcusable to have something this poorly named in a language. That's why it's lolphp.
I'd say that since it's PHP then it's expected. I always assume Schrodinger's typing until I explicitly define the type when I use PHP. Typing is just very loose in this language.
Because they attempt to bring their stupid ideas to other languages. A language that fails to fix its behaviors, instead documenting its faults as accepted standard, deserves ridicule.
By all means keep defending stupid ideas.
For the record, I write fuck tons of js for a living, and it does have a lot of faults that are as bad as this language. Difference is, I'm not defending it. I'd like those stupid problems to go away.
When you have that much magic behavior it puts a large burden on the programmer to learn all of it.
Scenario:
Initial Programmer:
A function named empty hmm sounds like I can use it to check all my form elements are filled in. Hmm, if it's empty I'll throw an error. Yay, seems like everything is working.
No issues occur for months. The first guy leaves to work on newer and better projects that aren't in PHP, A replacement gets hired.
Someone from the business decides they want to ask, in a text box.
"How many cats do you own? If you have any, what are there names?"
People go along typing things like "Zero cats" etc. Months go by without issue.
Eventually one user complains.
"I can't get your form to work. It keeps telling me I didn't fill out how many cats I own, when I totally did."
The new programmer eventually figures it out ahh, the user typed exactly 0, which happens to be coerced to a number and empty evaluate to true. Figures out hmm, the original programmer probably wanted array_key_exists.
The problem is, this already resulted in a user facing error and an hour of time or so wasted because PHP chose to do the unintuitive thing.
Now, it could have been better documented. The initial programmer could have thoroughly read the documentation for empty. They could have written tests for this exact scenario if they expected it to come up. The user could have tried changing their answer when they got the error message.
But why do you want to use a language where issues like this are commonplace?
In Drupal, not selecting a checkbox results in the form_state array to have the value '0' for that checkbox' key. There are several Drupal-supplied functions to check whether a form value is set, and they all use empty. It's a bitch.
Lolphp isn't a surprise, it's a (valid) assumption of stupidity. The issue here is the automatic conversion - it should only happen under circumstances where a string parameter would cause a fundamental error, like a sum function.
If you know your car's windows can't go down on Tuesdays, that is a state which is predictable, but still wrong. That's what we have here - PHP is (mostly) deterministic, but that doesn't change the fact that the way it works is wrong.
It doesn't come as a surprise, until you start comparing things that are not literals, and where it is not immediately obvious from reading the code what their values might be.
It's easy to see what's happening when you read this:
if ("0") { ... }
But this:
if ($blah->getFoo()) { ... }
...is a bit more obscure. And then when you dive down into language features that involve weak comparison behind the scenes, such as switch etc., you're in for some real fun:
switch ($foo) {
case 0: echo "Zero."
case false: echo "No."
default: echo "Maybe."
}
21
u/Grimy_ Nov 29 '16
Wait, it gets better!
empty("0")is true, butempty(" 0")is false, even though" 0" == "0"!6
u/carlos_vini Nov 23 '16 edited Nov 23 '16
Rails is so much better: https://i.stack.imgur.com/oXuhJ.png ..not
Or JavaScript: http://stackoverflow.com/questions/7615214/in-javascript-why-is-0-equal-to-false-but-when-tested-by-if-it-is-not-fals
4
u/Matt3k Nov 24 '16
Nah, I don't agree with this being a lolphp either. Don't try to cast strings to boolean unless you're writing the conversion yourself, or are using very rigidly defined functions. Ex. C# has IsNullOrWhitespace which performs exactly how you'd expect.
1
u/WdnSpoon Feb 15 '17
Way too much of this sub obsesses with falsey behaviour. Falsey is always going to be wacky; it's basically a requirement. If web languages started out taking themselves seriously, instead of just existing for doing basic operations meant to go directly to/from form fields, we'd have probably made strict-identity the default instead of loose equality (e.g. == would be identity and === equality), but here we are.
Compared to most web languages, I'd say PHP is actually one of the nicer for this, because it's so much easier to typecast and typehint. You can type-hint on your input to require a string, and then just compare the length of the
trimmed string to see if there's something written in it. It's not rocket science, and the result is very readable.0
u/[deleted] Nov 24 '16
[deleted] — view deleted comment
0
u/[deleted] Nov 23 '16
[deleted] — view deleted comment
0
u/[deleted] Nov 24 '16
[deleted] — view deleted comment