strlen and count in PHP5 – The Basics

If you are new to strlen and/or count in PHP, then you have come to the right place. In this article I will cover the basics you need to understand their uses, and move toward effectively implementing them in your own PHP projects. Both functions are used to count things, however what they count, and how they count them, are completely different. Understanding how they each work by themselves will allow you to use them together to accomplish some very useful things. However, for now, let’s start at the very beginning.

We will first examine strlen, which is a function for counting the number of characters in a string of text that you define. It can be used by either defining the string directly in the code, or by using variables. The syntax is fairly straightforward, with the code for establishing the string in question to be placed within opening and closing parenthesis, and any direct text placed within single or double quotations. PHP will not distinguish between the types of quotation marks you use, but as always it is recommended that you select one way of doing it and stay consistentthroughout your code.

String directly in code:

strlen ('13 characters');

Pulling string from a variable:

$stringVar = '13 characters';
strlen ($stringVar);

Note: The variable stringVar must be previously declared elsewhere in the code. This can be done by pulling values from a database, by combining multiple declared variables, getting values from an array, or declaring the the variable yourself.


The above code would both establish that there are 13 characters in that string of text. Spaces are counted as characters. You can output the number of characters in the string using “echo”. Doing so in either of the above cases would simply output the number ’13’ by itself.

You can make the output look a little bit nicer in ways like this:

$StringVar = 'some text';
echo 'Total: ' strlen ($StringVar) ' characters';
This would output the following text on your screen:

Total: 9 characters


For anyone wondering why they might want to count the characters in a string of text, that last example shows one very common use, displaying character counts in something that is being, or has been written. People often elect to use it alongside the sub-string selector, substr, to output only a portion of the string. You see this all the time with blogs, using “read more” links at the end of an introduction. In these kinds of cases, understanding strlen basics are essential.

Note: you cannot use strlen to count characters in an array without previously pulling the value from the array and storing it as a variable.

So if strlen counts characters, what does count do?

Simple, it counts the elements that are in an array, or it can also count the properties in an object.

There are a couple of options you can use when counting. As we know about arrays, you can have elements within the array which are arrays themselves. The two methods of counting are normal, and recursive. Recursive counting allows you to count each element within all of the arrays.

To illustrate this, imagine you are wanting to make an array containing every team from the National Basketball Association. You might like to separate the league into the Eastern and Western conferences. You could even take it a step further and separate each conference into the actual divisions, just like the NBA. For the purposes of this example, however, we will use the teams from the 2011 playoff conference semi-finals, separating them into East and West.

$playoffTeams = array('east' => array('boston', 'miami', 'chicago', 'atlanta'),
	'west'=> array('dallas', 'los angeles', 'memphis', 'oklahoma city'));

As you can see, in this example you are putting Boston, Miami, Chicago, and Atlanta as elements of a multi-layered array called ‘east’. Dallas, Los Angeles, Memphis, and Oklahoma city are also elements, but stored within the array called ‘west’. These two together make up the whole array, which is stored in the variable ‘$playoffTeams’.


Normal Count:

 echo count($playoffTeams);

This will only pull the two top-level elements, ‘east’ and ‘west’. The output would be seen in a browser as:



Recursive count:

echo count($playoffTeams, COUNT_RECURSIVE);

This will pull all of the elements within the array including all 4 teams from each conference, as well as the conferences themselves.


The output on your screen would be:


You should now understand the basic differences and uses of strlen and count. The next step would be learn more advanced ways of using them together. Go forth and program!