phpcoding-stylestrlenphp-internals

PHP String Length Without strlen()


Just browsing over the latest release of the PHP coding standards, and something caught my eye:

http://svn.php.net/viewvc/php/php-src/trunk/CODING_STANDARDS?revision=296679&view=markup

Coding standard #4 states that "When writing functions that deal with strings, be sure to remember that PHP holds the length property of each string, and that it shouldn't be calculated with strlen()..."

I've ALWAYS used strlen, and maybe it's just late, but how do you access the built-in length property of a string in PHP?


Solution

  • They're talking about the C function, not the PHP function. The C function will stop counting after the first \0, but PHP strings can contain \0 elsewhere other than the end.