Syntax
The syntax for the strlen() function in PHP is:
int strlen(string $string)
The strlen() function takes a single parameter, `$string`, which represents the string you want to measure the length of. The function returns an integer value indicating the number of bytes in the string.
It's important to remember that the strlen() function is case-sensitive. This means that uppercase & lowercase characters are treated as distinct bytes. Moreover, any whitespace characters, like spaces or tabs, are also counted as part of the string length.
Parameter Values
The strlen() function accepts a single parameter, `$string`, which represents the string you want to calculate the length of. Here are a few important points to note about the parameter:
1. The `$string` parameter is required. If you don't provide a string value, PHP will generate a warning & return `false`.
2. The `$string` parameter can be any valid PHP string, including string literals, variables, or expressions that evaluate to a string.
3. If you pass a non-string value, such as an integer or boolean, PHP will automatically convert it to a string before calculating the length.
For example :
strlen("Hello, world!"); // String literal
$message = "PHP is awesome!";
strlen($message); // String variable
strlen("10"); // String representation of a number
In each case, the strlen() function will return the number of bytes in the provided string.
Return Value
The strlen() function returns an integer value representing the length of the given string in bytes.
Few important points to keep in mind regarding the return value are:
1. The returned value is always a non-negative integer, as the length of a string cannot be negative.
2. If the given string is empty (i.e., ""), the strlen() function will return 0.
3. If the strlen() function encounters any invalid or malformed UTF-8 characters in the string, it will still count them as bytes & include them in the returned length.
For example :
echo strlen("Hello");
$str = "PHP";
echo strlen($str);
echo strlen("");
echo strlen("Été");

You can also try this code with Online PHP Compiler
Run Code
Output:
5
3
0
4 (multibyte characters)
In the last example, the string "Été" contains a multibyte character (é), which is represented by multiple bytes in UTF-8 encoding. As a result, the strlen() function returns 4, even though the string visually consists of only 3 characters.
Examples
Now let's take a look at some examples of using the strlen() function in PHP. These examples will help you understand how to apply the strlen() function in different scenarios.
Example 1: Basic usage
$string = "Hello, world!";
$length = strlen($string);
echo "The length of the string is: " . $length;

You can also try this code with Online PHP Compiler
Run Code
Output:
The length of the string is: 13
Example 2: Checking if a string meets a minimum length requirement
$password = "secret123";
$minLength = 8;
if (strlen($password) >= $minLength) {
echo "Password meets the minimum length requirement.";
} else {
echo "Password is too short. It must be at least " . $minLength . " characters long.";
}

You can also try this code with Online PHP Compiler
Run Code
Output:
Password meets the minimum length requirement.
Example 3: Truncating a string to a specific length
$longText = "This is a long string that needs to be truncated.";
$maxLength = 20;
if (strlen($longText) > $maxLength) {
$truncatedText = substr($longText, 0, $maxLength) . "...";
echo $truncatedText;
} else {
echo $longText;
}

You can also try this code with Online PHP Compiler
Run Code
Output:
This is a long strin...
In this example, we use the strlen() function in combination with the substr() function to truncate a string to a maximum length of 20 characters. If the string is longer than 20 characters, we extract the first 20 characters using substr() & append an ellipsis (...) to indicate that the string has been truncated.
Frequently Asked Questions
Does the strlen() function count whitespace characters?
Yes, the strlen() function counts all characters in a string, including whitespace characters like spaces, tabs, and line breaks.
Can the strlen() function handle multibyte characters?
Yes, the strlen() function can handle multibyte characters, but it returns the number of bytes rather than the number of characters.
What happens if you pass a non-string value to the strlen() function?
If you pass a non-string value to the strlen() function, PHP will automatically convert it to a string before calculating the length.
Conclusion
In this article, we discussed the strlen() function in PHP, which is used to calculate the length of a string. We learned about its syntax, parameter, return value, & saw different examples of how to use it in different scenarios. How to determine the length of a string is crucial for various string manipulation & validation tasks in PHP.
You can also check out our other blogs on Code360.