How to use PHP strlen to Get the String Length

The strlen function is used to get the length of a string in PHP programs.

The function returns bytes, unlike the general conception of returning the number of characters in the given string.

This is how you may use the strlen function to get string length:

int strlen ($string_to_get_length )
  • The strlen PHP function may be useful for different scenarios, for example, the HTML div should display only a limited number of characters from the user’s feedback, which is stored in a database.
  • You may get the length of string and do some programming to display only a limited string.

An example of PHP string length by using strlen function

In this example, a simple string is created which is assigned to a string variable.

After that, an int variable is used which is assigned the return value of strlen function for that string.

This is how the PHP strlen function is used in the above example:

$strstrlen = "This is strlen tutorial!";

$intlen = "The length of string is :" .strlen($strstrlen);

echo $intlen;

The output of the above example:

The length of string is :24

An example of having leading and trailing spaces in a string

PHP’s string length method, strlen, will treat leading or trailing spaces as length while returning the total length of the given string.

In this example, a leading and trailing space is added in the above example’s string. See what length is returned:

< ?php
    $strstrlen = " This is strlen tutorial! ";
    $intlen = "The length of string is :" .strlen($strstrlen);
    echo $intlen;
?>

Output:

The length of string is: 26

You see, it returned 26 because two spaces were added to the string.

What happens if you use arrays in strlen function

The strlen function works with strings. If you use an array in the strlen function, it will return null and emits a warning message.

See the output when an array is used in the function:

<!doctype html>
<html>

<head>

<style>
.divcls {
  background: #FF0000;
  height: auto;
  width:270px;
  border-radius: 15px;
  padding:20px;
  font-size:17px;
  color:#fff;   
}
</style>
 

  
</head>
<body>
<div class="divcls"><h3> A demo of strlen function:</h3><br /><br />
< ?php
    $arr = array ('a','b','c');
    $intlen = "The length of string is :" .strlen($arr);
    echo $intlen;
?>

</div>
</body>
</html>

Output:

PHP strlen arrays

You see, it returns null, and as such it is just a warning, the string in echo statement displayed and the program did not crash.

What if an array element is specified in strlen function

This time, an array is used in the strlen method, however, an element is given there with its index number.

The following array is created:

$arr = array (‘This’,’is’,’strlen’,’Tutorial’);

After that, the strlen method is used and see the output:

< ?php
    $arr = array ('This','is','strlen','Tutorial');
    $intlen = "The length of third array element is: " .strlen($arr[2]);
    echo $intlen;
?>

Output:

The length of third array element is: 6

The third element in the array is:

$arr[2] = ‘strlen’

As such, its length is 6, so strlen method returned the same.

An example of using integer in strlen function

If you give an integer variable in the strlen function to get the length of an integer variable, it will return its length.

The following example demonstrates that where 9 digit integer is given:

Code:

 $int_a = 123456789;

$intlen = "The length of 123456789 is : " .strlen($int_a);

echo $intlen;

Output:

The length of 123456789 is : 9

You see, the length of an integer variable with value 123456789 is returned as 9:

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!