PHP String Length Without strlen()

a 夏天 提交于 2019-12-23 17:07:28

问题


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?


回答1:


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.




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/4161977/php-string-length-without-strlen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!