PHP Strlen With Program Example

Definition and Usage

PHP has a built-in function called strlen() that tells us the length of a string.

It takes a string as an argument and returns the length of the string.

It determines the length of the string, including all the whitespace and special characters.

Additionally, according to the PHP documentation, strlen() returns the number of bytes rather than the number of characters in a string.

And strlen() returns null when executed on arrays, and a E_WARNING level error is emitted.

Syntax:

strlen( string )

Parameter Values in PHP Strlen()

The following are the parameters of PHP strlen() in PHP:

  • string – It’s a required parameter that tells the program what string to check.

Return Value

On success, the length of the string is returned; otherwise, 0 is returned.

Example of PHP Strlen()

Example 1:

The example that follows shows the use of PHP strlen() function.

<?php
    // declaration of string
    $str = "ItSourceCode: Where Source Code Is Not A Problem";
    // prints the original string
    echo("Original String: ".$str."<br>");
    // getting the length of the $str variable
    $sample_length = strlen($str);
    // prints the length of the string
    // including the space
    echo("String Length: ".$sample_length);
?>

Output:

Original String: ItSourceCode: Where Source Code Is Not A Problem
String Length: 48

Example 2:

This example shows how to use the strlen() method when a string contains special characters and escape sequences.

<?php
    // declaration of string
    $str = 'ItSourceCode\'s Motto: Where Source Code Is Not A Problem!';
    // prints the original string
    echo("Original String: ".$str."<br>");
    // getting the length of the $str variable
    $sample_length = strlen($str);
    // prints the length of the string
    // including the space and \' is counted as one
    echo("String Length: ".$sample_length);
?>

Output:

Original String: ItSourceCode's Motto: Where Source Code Is Not A Problem!
String Length: 57

Example 3:

In this example, we’re going to get the strength of the UTF-8 characters.

If our string has UTF-8 characters, the length of the string might not be what you expected.

This problem happens because strlen() counts the number of bytes, not the number of characters.

UTF-8 characters can have anywhere from 1 to 4 bytes, which can make a big difference in how long a string is as a whole.

We can solve this problem by using mb_strlen() , which counts characters instead of bytes, or by decoding the string before using strlen().

In the example below, “Hello World” is written as “Γειά σου Κόσμε“, which is Greek.

We show the output of our three different ways to find out how long the string is.

<?php
    // declaration of string
    $str = 'Γειά σου Κόσμε';
    // prints the original string
    echo("Original String: ".$str);
    echo("<br>");
    
    // getting the byte strength of the $str
    $using_strlen = strlen($str);
    echo("String Byte: ".$using_strlen);
    echo("<br>");
    
    // getting the characer length of the $str using mb_strlen
    $using_mb_strlen = mb_strlen($str);
    echo("Character Length (mb_strlen): ".$using_mb_strlen);
    echo("<br>");
    
    // getting the characer length of the $str using utf8_decode
    $using_decode = strlen(utf8_decode($str));
    echo("Character Length (utf8_decode): ".$using_decode);
?>

Output:

Original String: Γειά σου Κόσμε
String Byte: 26
Character Length (mb_strlen): 14
Character Length (utf8_decode): 14

Frequently Ask Question

Is strlen an int?

The return type of strlen() is an int long. The function sizeof() returns an unsigned int.

There is only one syntax for strlen(): int strlen(const char * str);

Does strlen count 0?

The strlen() function returns the string’s length if successful, or 0 if the string is empty.

Is strlen unsafe?

It is not safe to call strlen(). Unless you know for certain that the string is null-terminated, are all the functions you employ guaranteed to return a string that is null-terminated? char * strcpy(char * dst, const char * src);

Does strlen work with char *?

The strlen() function accepts an argument of type pointer to char or (char*), allowing you to supply either a string literal or a character array.

It returns the number of non-null characters in the string.

Summary

In summary, this tutorial has covered the basics of using the PHP strlen() function.

We addressed the basic usage of the function as well as how to manage strings containing UTF-8 characters.

It is a very helpful function that you will likely employ when working with strings in PHP.

Lastly, if you want to learn more about strlen() PHP, please leave a comment below. We’ll be happy to hear it!

Leave a Comment