开发者

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 p开发者_开发问答roperty of a string in PHP?


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.


Its been clearly mentioned that they talking about PHP Coding standards not about C function or extension of PHP engines.

========================
PHP Coding Standards
========================
This file lists several standards that any programmer, adding or changing code in PHP, should follow. Since this file was added at a very late stage of the development of PHP v3.0, the code base does not (yet) fully follow it, but it's going in that general direction. Since we are now well into the version 4 releases, many sections have been recoded to use these rules.

Still I didn't found any relevant information about string length property but I think in future they might release the information if it's related to new version of PHP.

Please post if someone found useful information about this.


To get the length of a string zval (variable in C) use Z_STRLEN(your_zval) see zend_operators.h at line 398 (PHP 5.4) :

#define Z_STRLEN(zval)          (zval).value.str.len
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜