php count string returns "1". Reason: count() is an array method that can return the number of elements in the array; when calculating "count(string)", the string will first be converted into an array with only 1 element, and then the number of elements in the array will be counted. and returns, thus returning the value "1".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
What does the php count string return?
Let’s take a look through the code:
<?php $str="hello"; echo count($str); ?>
The operation result is 1, so the count string returns the value "1".
Cause: Automatic type conversion occurred
count() is an array method that returns the number of elements in the array.
When using the count() function to calculate the length of a string, the system will automatically convert the string into an array with only 1 element array("hello")
, and then perform the calculation.
//等价于 echo count(array("hello"));
Thus count(string) will return the value 1.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What does php count string return?. For more information, please follow other related articles on the PHP Chinese website!