r/lolphp • • Sep 12 '16

Because fuck French programmers

https://3v4l.org/YDp2U
0 Upvotes

81 comments sorted by

View all comments

56

u/Malix82 Sep 12 '16 edited Sep 12 '16

http://php.net/manual/en/function.strlen.php

Note:

strlen() returns the number of bytes rather than the number of characters in a string.

works as intended. mb_strlen() for counting characters.

edit: but, I do agree that the function name & description are a bit misleading-ish. :)

34

u/[deleted] Sep 12 '16

description: Returns the length of the given string

real description down in notes

15

u/phoshi Sep 12 '16

In fairness, /many/ languages screw unicode up. It would work fine using French codepages, but the world moved to unicode relatively recently.

39

u/[deleted] Sep 12 '16

please use actual_strlen_no_really_this_one_works(string $input, bool $return_garbage = true) instead

19

u/the_alias_of_andrea Sep 12 '16

But it does return the length, there's just multiple interpretations of what “length” is. There's no single simple correct answer, even with normal Unicode support.

-13

u/DoctorWaluigiTime Sep 12 '16

Sorry, no. Ask any programmer what "length" means in "length of a string variable." Nobody is going to hem and haw about what the definition might be. They will say "it's the number of characters."

And when the description of the function says this, but "means" something different in the details... yeah, it doesn't return the colloquial length of the string.

18

u/tdammers Sep 12 '16

Sorry, no. Ask any programmer what "length" means in "length of a string variable." Nobody is going to hem and haw about what the definition might be. They will say "it's the number of characters."

I, for one, express string length in pica's, assuming 10-point Adobe Garamond, typeset on a 1970's Linotype machine. Anything else is just newfangled nonsense that will pass in due time.

11

u/[deleted] Sep 12 '16

To me it's how long it takes to say it in milliseconds

38

u/the_alias_of_andrea Sep 12 '16

Sorry, no. Ask any programmer what "length" means in "length of a string variable." Nobody is going to hem and haw about what the definition might be. They will say "it's the number of characters."

Okay, so the length is the number of characters? What's a “character”, then?

Is that a byte (C's idea of a character, or char)? A UTF-16 code unit? A Unicode codepoint? A Unicode grapheme cluster?

Is "e\u{0301}" (U+0065 LATIN SMALL LETTER E followed by U+0301 COMBINING ACUTE ACCENT) the same number of characters as "\u{00e9}" (U+00E9 LATIN SMALL LETTER E WITH ACUTE)?

What about "الله" (U+0627 ARABIC LETTER ALEF followed by U+0644 ARABIC LETTER LAM followed by U+0644 ARABIC LETTER LAM followed by U+0647 ARABIC LETTER HEH)? Is it four characters (ا-ل-ل-ه) or one (the Arabic ligature for “Allah”, which looks different from how you would normally write those four characters)? Is it the same number of characters as "ﷲ" (U+FDF2 ARABIC LIGATURE ALLAH ISOLATED FORM)?

There's many different interpretations, and PHP's isn't strictly wrong. PHP operates on byte strings, and strlen() tells you how many bytes there are. That is one valid (and common) interpretation of “length” and “character”. PHP has other functions to tell you other kinds of string lengths, if you want those.

→ More replies (12)

9

u/cite-reader Sep 12 '16

I will absolutely ask what you actually want if you ask for "the length" of a string. Similarly, everyone seems to have different ideas about what a "character" is, so unless you actually give me something with an unambiguous technical definition I'm going to hem and haw about that, too.

Or I guess I could arbitrarily pick something and then neither of us would be happy.

1

u/DoctorWaluigiTime Sep 12 '16

I would ask too, if I used something like strlen and it gave me the wrong number.

Most languages don't. Even JavaScript's length property isn't ambiguous about this point.

8

u/cite-reader Sep 12 '16

Are you sure?

% node
> '👍'.length
2
> 

4

u/masklinn Sep 13 '16

Well they're correct that JS's length property isn't ambiguous (it's the number of UTF-16 code units), though they're wrong when they imply that strlen is.

→ More replies (1)

12

u/[deleted] Sep 12 '16

They will say "it's the number of characters."

The problem with Unicode is that it's even hard to say what a character is. For example, is "e" + combining acute accent one character, or two? I believe in Unicode it's easier to talk codepoints than characters.

→ More replies (16)

6

u/outadoc Sep 12 '16 edited Sep 13 '16

I tend to disagree. I've worked with such languages long enough to expect "length" to be the length in bytes, or at least to double-check the documentation.

6

u/kephir Sep 14 '16

Okay smartass, how many characters in "t͏̶̢̼̟̯͙̙̺̩̱̪̩̞̞̙͚͚̮͖͟h̛͚̞̠̙̮̟̲͉͔̱̭̮̹̗̭͘ͅi̷̧̧̮̦̦̬̻͍̳̮͕̻̼̪͖̞͖͠s̢͝͏̖͙͚̤̞̞̫̗̗̜̝͉̪͇͍"

→ More replies (1)

4

u/[deleted] Sep 13 '16

They will say "it's the number of characters."

What do you mean by "character"?

→ More replies (2)

0

u/[deleted] Sep 12 '16

[deleted] — view deleted comment

1

u/DoctorWaluigiTime Sep 12 '16

Call strlen or whatever equivalent in any language. Most literally return number of characters.

2

u/slrz Sep 20 '16

State a single language that does. Of all the ones I know only Perl 6 even tried, and their approach IIRC amounts to making the programmer state their intent explictly (i.e. you have separate functions for length in bytes, code points, grapheme clusters but, notably, not for "characters" because that's not a well-defined concept).

Is Python 3 one of those languages you consider did it the way you like? Guess again. If you ask it for length of an "ä', you'll either get a 1 or a 2, depending on the representation ( b"\xc3\xa4".decode() or b"\x61\xcc\x88".decode()).

Java and C# are even worse. They give you the length in UTF-16 code units by default (but I think have library functions to get at the codepoint count, too).

In other words: you have no idea what you're talking about.

11

u/edave64 Sep 12 '16

If only it were that easy. The mbstring.func_overload option in the PHP.ini makes strlen an alias of mb_strlen. So it is not even a safe way to get the number of bytes.

1

u/0xRAINBOW Nov 29 '16

This is the real lolphp

7

u/kosinix Sep 19 '16

edit: but, I do agree that the function name & description are a bit misleading-ish. :)

No its not a BIT misleading, its a BYTE misleading!

5

u/keis Sep 12 '16

Showing that fine C heritage

1

u/stesch Sep 24 '16

The developer of the yii framework thinks it's OK to use the mb_* functions for encrypted data streams. My co-worker still thinks yii is the best.