AppDividend
Latest Code Tutorials

PHP Strlen Example | strlen() Function Tutorial

PHP Strlen Example | strlen() Function Tutorial is today’s topic. The strlen() function returns a length of the string. Returns a length of the string on success, and 0 if the string is empty. It takes a string as a parameter and returns its length. The strlen() function calculates a length of a string, including all the whitespaces and special characters.

PHP Strlen Example

See the following syntax of the strlen() function.

strlen(string)

The string parameter is required. Specifies the string to check.

<?php

$character = 'Dustin Henderson';
echo strlen($character);

See the output.

➜  pro php app.php
16                                                                              
➜  pro

PHP strlen() returns the number of bytes rather than the number of characters in a string.

The strlen() returns NULL when executed on arrays, and an E_WARNING level error is emitted.

#Empty String

Let’s check with the empty string.

<?php

$character = '';
echo strlen($character);

See the output.

➜  pro php app.php
0                                                                               
➜  pro

#Null Value

Let’s assign the null value and then see the output.

<?php

$character = null;
echo strlen($character);

See the output.

➜  pro php app.php
0                                                                               
➜  pro

So, as per the manual: “PHP strlen() returns the number of bytes rather than the number of characters in the string.”, so if you want to get a number of characters in a string of UTF8 so use mb_strlen() instead of strlen().

See the following example.

<?php

$arabic = "السلام علیکم ورحمة الله وبرکاته!";
echo (strlen($arabic))."\n";
echo mb_strlen($arabic, "utf8");

In the above code, we have used the two different functions.

  1. strlen
  2. mb_strlen

Both functions will give us a different output.

➜  pro php app.php
59
32                                                                              
➜  pro

See below another typical example.

<?php

$kiki = "bär";
echo (strlen($kiki))."\n";
echo mb_strlen($kiki, "utf8");

See the following output.

➜  pro php app.php
4
3                                                                               
➜  pro

So, if you are working languages like Arabic, Persian, Pashto, Dari, Chinese (simplified), Chinese (Traditional), Japanese, Vietnamese, Urdu, Macedonian, Lithuanian, etc then use the mb_strlen() function.

#Whitespace in strlen()

Let’s check the string with only white space.

<?php

$kiki = '                ';
echo strlen($kiki);

See the following output.

➜  pro php app.php
16                                                                              
➜  pro

That means whitespaces are count inside the string even if the string has no single character but spaces.

Finally, PHP Strlen Example | strlen() Function Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.