Fri, 17 May 2024


PHP Count Function

By: Anton, NetArt Media
Wed, 1 August 2018
PHP Count Function

From the moment we wake to the time we're going to bed, we humans are consistently counting. Whether it's minutes on the clock to make sure we meet our morning deadlines or counting sheep when trying to fall asleep, it is a constant in our lives. Therefore, it would only stand to reason that being able to count objects, whether of the array or string variety, is quite a relevant task when coding with PHP.

Today, I feature a short article on using the PHP count function, which works with arrays, and the substr_count function, which, of course, works with strings. I will first create an array of different types of fruit, returning it according to it's incidental count. Then, I will create a string with the same values inside it, but use substr_count to deliver the same results as I did with the array.


Now, when using the count function with arrays sometimes we have to manipulate it a bit in order to return a quick and correct result. In this case, I chose to subtract 1 digit from the count because I used offset to display the 4 different types of fruit. Count gets the sum of all the keys within the array. In this example, there would be 4. However, offsets start at '0', not '1', which means the count would be 1 digit past the offset. If we were to use 'count' as it is within the while statement, PHP would return with an undefined offset error. Using subtraction prevented the error and worked well for this particular returning of data.

Using the increment Operator, I was able to loop through the 4 fruit types and return them accordingly. By simply using 'count($fruit)' we are able to get the exact number of instances within the array. Here is the result of this code block...

We have 4 types of fruit on display today.

We have...
oranges
bananas
apples
raspberries

Now let's see if we can achieve the same result with a string of fruit types.


I took an interesting route with this string of fruit. There are dozens of ways a person could go about getting the count of the number of words in the string. I tried something a bit unique and simply counted the number of empty spaces within the string. By only putting a space between each word and there being 4 words in total, the space count would be 3. Therefore, it would stand to reason that 3 + 1 will give us the actual count of words in the string.

Then, by replacing each empty space with a break tag, we can return each particular fruit on it's own line. Just like we accomplished when manipulating the array. The result of this code block is below.

We have 4 types of fruit on display today.

We have...
oranges
bananas
apples
raspberries

It's exactly the same as the array result. :-) Pretty cool, huh? I do get my jollies messing around with things like this and it really does prepare a coder for times when we may have to do something out of the ordinary in order to achieve a particular result. Trust me, it happens more often than you may think.


Category: Web Development
Share this post:



See All Scripts






Subscribe for our newsletter

Receive the latest blog posts direct in your mailbox