Home > Article > Backend Development > Two ways to determine the length of a string in PHP
This article mainly introduces relevant information on PHP's judgment of string length. Friends who need it can refer to the
string length judgment in the php program. You can use strlen .
Method one:
$str = ‘aaaaaa'; if(strlen($str) > 6){ echo "字符串大于6"; }
Method two:
if(isset($str{6}){ }
The second method is more efficient.
In PHP, all variables are saved using a structure -zval. Although strlen directly obtains the len in it, there is still a function Call, and isset is a syntax structure of PHP, so it is faster! So the second method can be used when determining whether a string is greater than or less than a number of characters.
The above is the detailed content of Two ways to determine the length of a string in PHP. For more information, please follow other related articles on the PHP Chinese website!