Using PHP strlen() to Get String Length

This article explains PHP's strlen() function, detailing its syntax, return value, and providing multiple code examples that demonstrate how to obtain a string's length, check for emptiness, enforce length limits, and handle multibyte characters, while noting the use of mb_strlen() for Unicode strings.

php Courses
php Courses
php Courses
Using PHP strlen() to Get String Length

In PHP development we often need to obtain the length of a string. PHP provides the useful function strlen() for this purpose. This article introduces its usage and provides several example codes.

Syntax: int strlen ( string $string ) The function accepts one parameter—the string to measure—and returns its length in bytes.

Below are some common usage examples:

1. Getting the length of a string

$foo = "Hello, World!";
$length = strlen($foo);
echo "The length of the string is: " . $length; // Output: The length of the string is: 13

2. Checking if a string is empty

$bar = "";
if (strlen($bar) == 0) {
    echo "String is empty";
} else {
    echo "String is not empty";
}

3. Determining if a string exceeds a length limit

$password = "abcd";
$length = strlen($password);
if ($length > 8) {
    echo "Password is too long, please re-enter";
} else {
    echo "Password length is acceptable";
}

4. Getting the length of multibyte characters

$chinese = "你好,世界!";
$length = strlen($chinese);
echo "The length of the string is: " . $length; // Output: The length of the string is: 15

It is important to note that strlen() counts bytes, not characters. For strings containing multibyte characters (such as Chinese), each character occupies multiple bytes, so the byte length may exceed the character count. To obtain the number of characters, use mb_strlen() instead.

Measuring string length is commonly used for validating user input, processing strings, and performing various comparisons. By using strlen(), developers can easily retrieve a string's length and carry out subsequent operations.

Summary:

This article introduced PHP's strlen() function, which returns the length of a string in bytes. Through several code examples we demonstrated how to use the function to check if a string is empty, enforce length constraints, and obtain the length of strings containing multibyte characters. Understanding this function is essential for string manipulation and input validation in PHP development.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendTutorialstring lengthstrlen
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.