Home  >  Article  >  php教程  >  PHP判断字符串长度的两种方法很实用

PHP判断字符串长度的两种方法很实用

WBOY
WBOYOriginal
2016-06-06 19:44:181631browse

这篇文章主要介绍了PHP判断字符串长度的相关资料,需要的朋友可以参考下

php程序中字符串长度判断,,可以使用strlen。

方法一:

$str = ‘aaaaaa'; if(strlen($str) > 6){ echo "字符串大于6"; }

方法二:

if(isset($str{6}){ }

以上两种方法,第二种效率更高些。

在PHP中,所有的变量都是用一个结构-zval来保存的,strlen虽然是直接获取其中的len,但是仍然有一次函数调用,而isset是PHP的语法结构,所以更快!所以在判断字符串是否大于或小于多少个字符时可以使用第二种方法。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn