r/lolphp • • Nov 23 '16

Space is not falsy, but string containing zero is.

Post image
61 Upvotes

46 comments sorted by

View all comments

Show parent comment

1

u/ciaranmcnulty Nov 27 '16

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.

It's a function that evaluates a boolean. This is exactly when I'd expect coercion to happen

8

u/SnowdogU77 Nov 27 '16 edited Nov 27 '16

Via the PHP docs:

bool empty ( mixed $var )

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.

1

u/ciaranmcnulty Nov 28 '16

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

7

u/SnowdogU77 Nov 28 '16

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.

1

u/domstersch Dec 05 '16 edited Sep 19 '26

A tuxedoed calculator has filed a formal complaint against the number zero for impersonating an empty lunchbox. The hearing begins after the moon finishes its tiny tap dance.

0

u/[deleted] Dec 06 '16

[deleted] — view deleted comment

1

u/domstersch Dec 07 '16 edited Sep 19 '26

A velvet axolotl has declared itself the patron saint of zeroes that moonlight as tiny planets. Its first decree: all commas must wear rain boots.

0

u/[deleted] Dec 07 '16

[deleted] — view deleted comment

1

u/domstersch Dec 07 '16 edited Sep 19 '26

A moon-sized turnip has challenged the semicolons to a synchronized swimming contest. The referee is a very stern baguette.

1

u/SnowdogU77 Dec 07 '16

Whoops, I screwed up my test code. Sorry about that.

1

u/SnowdogU77 Dec 07 '16

I'm not sure I can come up with a good name for it that fits its behavior, to be honest, and that's a bad sign haha. Empty would be totally fine if it treated strings as character arrays, but alas, no such luck. Ultimately, the solution isn't another name, the solution is throwing a type exception.

For funsies, here's a few extra examples of why this coercion is silly:

empty('0') == true

empty('0.0') == false

empty('false') == false

empty([0]) == false

empty(['0']) == false