chacham15
chacham15

Reputation: 14251

Why does PHP have no length argument on functions which deal with binary data?

Why is it that functions that deal with binary data dont have length arguments? I come from a C background, so this is really confusing me. I thought that perhaps the string object holds the length, but that doesnt appear to be the case (correct me if im wrong). Two examples of functions that exhibit this behavior are base64_encode/decode and bin2hex.

Thanks!

Upvotes: 2

Views: 143

Answers (2)

phihag
phihag

Reputation: 288070

php's string objects hold the length internally. You can get this value by calling strlen, but since php has automatic management, you rarely need to worry about that:

echo strlen("a\0b\0c"); // 5

Upvotes: 2

timdev
timdev

Reputation: 62894

PHP strings are not like C-strings.

It's been a while since I thought about the internals. As I remember, php variables are known as "zvals", which is basically a struct that tracks things like the datatype, length, and the data themselves.

So internally (at least down to a pretty low level), PHP probably avoids relying on null-termination to know where a string ends.

EDIT: Here's an old article that discusses some of the internal implementation of userspace variables in PHP

Upvotes: 2

Related Questions