How to Find String Length Using PHP strlen function

Last Updated on December 30, 2020 by Roshan Parihar

In this tutorial, learn how to find string length with PHP strlen(). The short answer is: pass the string variable as the argument of PHP strlen() to get the length of the string.

How to Find String Length With strlen()

To find the length of any string in PHP, you have to use the PHP strlen(). The function takes the string variable as an argument to get the size of the string. It also considers the spaces to get the size of the string.

Output

24

The above example contains a single string that is stored in a string variable $mystr. After that, you can use the PHP strlen() and pass the string variable as an argument. The example prints the size using the PHP echo statement to get the output.

Get String Size With More than one Space

If the string contains extra spaces within the words, the PHP strlen() consider all the spaces to get the exact string length.

The below example contains the 5 spaces between the string words and there are 12 characters in the string.

Output

17

The above example prints the number of characters in a string including all spaces.

I hope you like this post on how to get the string size in PHP.

Reference
PHP.net Doc