How to get the length of a string in PHP?
In PHP, you can use the strlen() function to retrieve the length of a string. This function returns the number of characters in the string, regardless of the encoding.
Here is an example:
$str = "Hello World";
$length = strlen($str);
echo "字符串的长度为:" . $length;
The output yields:
字符串的长度为:11
Note: If the string contains multi-byte characters such as Chinese, the strlen() function will count each byte, which may lead to inaccurate results. When dealing with multi-byte characters, you can use the mb_strlen() function to calculate the length of the string.