How to get the length of an array in PHP

Summary:

Use the count() or sizeof() function if you want to get the length of an array in PHP. They are the same function under the hood with sizeof just being an alias. We recommend using the count function.

PHP Array Length – What is the right function to use: count vs sizeof

I recommend using the PHP count function to get the length of an array. You can use the sizeof() function as well but it’s only an alias of count() so it makes sense to use the original.

Using count to check the length of a PHP array

The count function is the primary function within PHP to check the length of an array or PHP countable object. It has existed since the original PHP 4 days from the 90s. It is standard and people know what it means when they read it. It takes 2 parameters (1 required) to use. The first parameter is the array and then the second is an optional mode constant (COUNT_NORMAL or COUNT_RECURSIVE). I use this function on a daily basis.

Examples:

<?php 

// Echo the length of my pies
 $pies = ["apple", "blueberry", "cherry", "pecan"];
 echo "I have " . count($pies) . " total pies";
 // renders - 'I have 4 total pies
<?php 
 
// Loop through my pies and let the world know what pies I have.
$pies = ["apple", "blueberry", "cherry", "pecan"];

for ($i = 0; i < count($pies); i++) {
 echo "I like $pies[i] pie and I cannot lie. <br />";
}
// Renders:
// I like apple pie and I cannot lie.
// I like blueberry pie and I cannot lie.
// I like cherry pie and I cannot lie.
// I like pecan pie and I cannot lie.

Using sizeof to get the length of a PHP array

There is another function that you can use to get the length of a PHP array. sizeof will do the exact same thing as the count function. In fact, it is actually an alias of count and runs the same logic as the other function. It was also introduced in the PHP 4 days of the 90s. I don’t know why they decided to create this alias function at the same time as the count function. Maybe sizeof was a popular function to get the length of a PHP array in another language at the time? Either way, I believe they should deprecate this function.

Examples:

<?php 

// Echo the length of my pies
 $pies = ["apple", "blueberry", "cherry", "pecan"];
 echo "I have " . sizeof($pies) . " total pies";
 // renders - 'I have 4 total pies
<?php 
 
// Loop through my pies and let the world know what pies I have.
$pies = ["apple", "blueberry", "cherry", "pecan"];

for ($i = 0; i < sizeof($pies); i++) {
 echo "I like $pies[i] pie and I cannot lie. <br />";
}
// Renders:
// I like apple pie and I cannot lie.
// I like blueberry pie and I cannot lie.
// I like cherry pie and I cannot lie.
// I like pecan pie and I cannot lie.

Why count is better than sizeof to get the PHP array length

You may have realized from above that my preference is for count over sizeof. There are 3 reasons why you should stick with the count function and ignore the sizeof function. It comes down to readability, expectations and originality.

  1. Readability: Most developers know and use the count function for array length every day of their programming life. They know it when scanning your code and that’s important. Sizeof is used less so expect some programmers to switch to Google to search “what is sizeof in PHP” if you use that function.
  2. Expectations: This goes with point #1. You can easily understand what is happening when you read the count function. On the other hand, some languages use sizeof to display the memory size instead of the element length. Confuse less people by using count.
  3. Originality: count is the original function. sizeof is just an alias. Aliases are useful whenever they make confusing things less confusing. There’s nothing confusing about count. Go with the obvious and the original.

Bonus: Did you know that you can use count() to get the length of your multidimensional array?

<?php 

$myArray = ["a" => ["apple", "attitude", "answers"]];

echo count($myArray);
// displays 1

echo count($myArray, COUNT_RECURSIVE);
// displays 4

I generally use the count function when working with PHP arrays to run loops or check for expected data lengths. It’s a great simple function for this need, but many people don’t know that it has more capabilities than you know.

You can actually assign the second parameter in count to value COUNT_RECURSIVE and it will count the array recursively so you will get a total of all of the keys throughout the entire multidimensional array.

I find that ability to count a PHP array recursively is a really nice-to-know item, but not widely needed. There aren’t many use cases where I’ve needed to know the count of all the elements within my multidimensional tree, but I can really see the value in it if / when it does happen. If I needed to do data validation on API results then it could be really beneficial when I know the exact tree length to expect.

Dear PHP arrays, why do you need to be greedy and have 2 functions to get your length?

I don’t know what the thinking was whenever they decided to have multiple functions to do the same thing in PHP. It is confusing and adds more to learn and know whenever you are writing and reading code. That isn’t ideal. I recommend always using the count function whenever you need to know the length of a PHP array. If you ever come across sizeof , please find the person who committed the code and take their ice cream. They don’t deserve it.

Dig deeper into PHP Array Lengths:


Article Categories

Article Tags

    © 2022 Confident.Systems