The strlen() function is used to return the length of a specific string. This function was introduced in PHP3. The strlen() function takes just one parameter (the string) and returns the number of characters in it.
Syntax:
strlen(string $string): int
Behind the scenes, strlen() actually counts the number of bytes in your string, as opposed to the number of characters. It is for this reason that multibyte strings should be measured with mb_strlen().
Parameters
Parameter | Description |
---|---|
string | The string being measured for length. |
Return Values
Returns the number of characters in a string, and 0 if the string is empty.
Examples
Example 1:
print strlen("Foo") . "\n"; // 3 print strlen("Goodbye, Perl!") . "\n"; // 14
Example 2:
<?php $text = 'aeiou'; echo strlen($text)."<br>"; $str = ' ab cd '; echo strlen($str); ?>
The output of the above program is:
5 7
Example 3:
<?php $str = 'abcdef'; echo strlen($str); // 6 $str = ' ab cd '; echo strlen($str); // 7 ?>