Programming language on computer screen with toy elephant facing it. php displayed on elephant

Have you ever needed to get the length of a string? PHP makes the process of getting string length easy with the PHP STRLEN (PHP string length) function. Today, we’re going to look at the PHP STRLEN function, when you would use it, and what other functions you might need to use to manipulate strings in PHP.

What is the PHP string length Function?

In PHP, the STRLEN function will simply return the length of a string. PHP strings, like most code strings, are really arrays. So, the PHP string “Hello, World!” is an array of 13 characters, starting with the number 0. The built-in function PHP STRLEN returns the number of characters in the string.

The following code:

<?PHP
	print(strlen("Hello, world!"));
	?>

Will return the following result:

	13

So, you use STRLEN any time you need the length of a string. The STRLEN string can also pass as a variable:

	<?PHP
		$hello = "Hello, world!";
		print(strlen($hello));
	?>

And since the PHP STRLEN function has a function return and returns the number, you don’t need to print the STRLEN. You could also save the number of characters to a variable, like so:

<?PHP
		$hello = "Hello, world!";
		$string_length = strlen($hello);
	?>

This doesn’t print anything, but it does store the STRLEN string’s length into the variable $string_length for use later.

PHP for Beginners – Become a PHP Master – CMS Project

Last Updated October 2022

Bestseller
  • 336 lectures
  • All Levels
4.3 (24,097)

PHP for Beginners: learn everything you need to become a professional PHP developer with practical exercises & projects. | By Edwin Diaz, Coding Faculty Solutions

Explore Course

What happens if the string is empty?

If the string is empty, the STRLEN function will simply return 0. So consider the following code:

Programming language example: "Hello"  Checking to see whether a variable is assigned.

You can use STRLEN to determine whether a variable is assigned by checking if it’s equal to 0. 

Note that if the variable isn’t empty but is instead null, an error will be thrown because you can’t use the function on a null value.

When would you need to know PHP string length?

PHP string length is most commonly used to validate form entries. While you can validate it in the front-end, you usually need to validate it again in the back-end. Consider that you might have a $META_DESCRIPTION variable that has to be less than 155 characters but more than 40 characters.

<?PHP
		$meta_description = "Learn about PHP! PHP is a fast-growing language and an exciting career.";
		$meta_length = strlen($meta_description);

		if($meta_length < 40) { 
			print("Your meta description is too short.");
		} elseif ($meta_length > 155) { 
			print("Your meta description is too long.");
		} else {
			print("Your meta description is just right.");
		}
	?>
Coded language PHP string length breakdown.

As you can see, it’s easy to identify whether the meta description is the correct length using the STRLEN function.

You can validate arguments before sending them to other functions. For instance, the PHP SETCOOKIE function sets a cookie on a user’s device, but you don’t want to send a lot of garbage to the cookie. You can make sure the path, domain, and other arguments aren’t too long before sending them in.

Finally, PHP STRLEN is frequently used when validating entries into a database, such as a MySQL database. If you’re entering an email address into a database and the database can only contain CHAR(50), you need to make sure the email address is less than 50 characters. Otherwise, your database entry will fail.

Issues with the PHP STRLEN function

Earlier, we said that this function returns the number of characters. That’s technically true, but more specifically, it returns the number of bytes. Nearly always, that’s going to be the number of characters in the string.

But an issue occurs when you use UTF-8 characters, which will count as multiple characters. For instance, a u with an umlaut ü is going to count as two characters. 

So, let’s replace the “Hello, world!” text, which we know has 13 characters, with “Hellö, wörld!”:

looking at UTF-8 characters in PHP’s STRLEN function.

Oops! Now, most people don’t need to deal with UTF-8 characters frequently. But any programmer knows that you can’t create a system that breaks “sometimes.”

That’s why people frequently use the MB_STRLEN function rather than the STRLEN function.

Alternatives to the PHP STRLEN function

While PHP STRLEN is relatively straightforward, there are three notable alternatives to PHP STRLEN: MB_STRLEN and ISSET. Let’s discuss the differences between these alternatives.

Top courses in PHP

APIs in PHP: from Basic to Advanced
Dave Hollingworth
4.6 (792)
Laravel 10 – Build Real Estate Property Listing Project A-Z
Kazi Ariyan, easy Learning
4.8 (135)
Highest Rated
PHP for Beginners – Become a PHP Master – CMS Project
Edwin Diaz, Coding Faculty Solutions
4.3 (24,095)
Bestseller
PHP with Laravel for beginners – Become a Master in Laravel
Edwin Diaz, Coding Faculty Solutions
4.4 (12,504)
Object Oriented PHP & MVC
Brad Traversy
4.5 (4,826)

More PHP Courses

MB_STRLEN

MB_STRLEN is the most commonly used alternative to PHP STRLEN. It’s used the same way as STRLEN, except there’s an additional parameter called “encoding.” The additional encoding parameter tells the compiler what type of string is being counted.

In general, MB_STRLEN is considered the correct way to count strings, whereas STRLEN is now considered somewhat deprecated. But note that many programs rely on STRLEN because it’s been in use for some time and because MB_STRLEN wasn’t introduced until PHP4.

ISSET

When trying to determine whether a variable is set, it’s also better to forego the PHP STRLEN function and simply use the ISSET function.

<?PHP
		$hello_world = 1;
		if(isset($hello_world)) {
			print("Hello, world!");
		}
	?>

In the above example, “Hello, world!” is printed. But if the $hello_world variable was not set, then it would not have printed. Moreover, if the $hello_world variable is called within ISSET after having never been set, it doesn’t throw an error. If you use STRLEN with a variable that has never been set, it will throw a warning. 

There are also functions like the COUNT_CHARS function, which counts the instances of a given character within a string. And there’s STR_WORD_COUNT, which will count the individual words within a string (although it’s not entirely reliable).

Using the PHP STRLEN function

PHP STRLEN, or PHP string length, is a great built-in function with a lot of utility. Anytime you need to know the number of characters in a string, you can use PHP STRLEN. Once it returns the number, you can use that number in other calculations.
Do you want to know more about the basics of PHP functions, string length, and string operations? The easiest way to do so is to learn more about PHP programming. You can join a PHP course or start a portfolio project as your next step.

Page Last Updated: May 2014