In PHP what does it mean by a function being binary-safe?


Answers

The other users already mentioned what binary safe means in general.

In PHP, the meaning is more specific, referring only to what Michael gives as an example.

All strings in PHP have a length associated, which are the number of bytes that compose it. When a function manipulates a string, it can either:

  1. Rely on that length meta-data.
  2. Rely on the string being null-terminated, i.e., that after the data that is actually part of the string, a byte with value 0 will appear.

It's also true that all string PHP variables manipulated by the engine are also null-terminated. The problem with functions that rely on 2., is that, if the string itself contains a byte with value 0, the function that's manipulating it will think the string has ended at that point and will ignore everything after that.

For instance, if PHP's strlen function worked like C standard library strlen, the result here would be wrong:

$str = "abc\x00abc";
echo strlen($str); //gives 7, not 3!
Question

In PHP what does it mean by a function being binary-safe ?

What makes them special and where are they typically used ?





Tags