Home > Backend Development > PHP Tutorial > PHP判断字符串长度的两种方法很实用_PHP

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

WBOY
Release: 2016-05-29 11:48:25
Original
1052 people have browsed it

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

方法一:

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

方法二:

if(isset($str{6}){
}
Copy after login

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

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template