Join my Laravel for REST API's course on Udemy 👀

Get the length of a string in PHP

March 13, 2021  ‐ 1 min read

This might be one of the easiest things you'll ever do, in PHP to be precise. To get the length of a string in PHP you use the strlen() function. It takes a string as its argument and returns the length of the string, the amount of individual characters with spaces, tabs, line breaks, etc. included.


echo strlen('four');
# => 4

Easy peasy.

In case you want to check if a string is empty you could better use the empty() function. Which checks whether a variable is empty.