Table of contents
1.
Introduction
2.
Definition & Usage
3.
Syntax
4.
Parameter Values
5.
Return Value
6.
Examples
6.1.
Example 1: Basic usage
6.2.
Example 2: Checking if a string meets a minimum length requirement
6.3.
Example 3: Truncating a string to a specific length
7.
Frequently Asked Questions
7.1.
Does the strlen() function count whitespace characters?
7.2.
Can the strlen() function handle multibyte characters?
7.3.
What happens if you pass a non-string value to the strlen() function?
8.
Conclusion
Last Updated: Oct 23, 2024
Easy

PHP strlen() Function

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In PHP, strings are a fundamental data type which are used to store & manipulate text. One of the important aspects of working with strings is determining their length. The strlen() function in PHP provides a simple way to find the length of a string, which is useful in various scenarios like validating user input, formatting output, or processing data. 

PHP strlen() Function

In this article, we will discuss the strlen() function, its syntax, parameters, return value, & practical examples to help you understand how to effectively. 

Definition & Usage

The strlen() function in PHP is used to calculate the length of a string. It returns the number of bytes in the given string. It's important to note that the strlen() function counts the number of bytes rather than the number of characters. This distinction is crucial when we are working with multibyte character encodings like UTF-8, where a single character may be represented by multiple bytes.

The strlen() function is part of PHP's core string functions & is widely used for string manipulation & validation tasks. Whether you need to check if a user's input meets a certain length requirement or you want to truncate a string to a specific size, the strlen() function is a useful tool in PHP.

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.

Live masterclass