Array Length in PHP: Use the Count Function with Code Examples

Last Updated on

CraftyTechie is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

How to Get the Length of an Array in PHP

PHP provides two identical functions whenever you need to get the length of an array. The count function is the most common method. You can also use the sizeOf function as it is just an alias of the count. I prefer the count function and would recommend avoiding the sizeOf function.

PHP Count Function Code Example

<?php
//An array with many elements.
$arr = array("PHP","Javascript","Python","Java","C#","C++");
 
//Let's count this array using the count function and print it to console.
echo "Length of \$arr is: ".count($arr);
 
//OUTPUT
//Length of $arr is: 6
?>

Counting Arrays in PHP

An array is a data structure to store multiple elements. Like any programming language, PHP provides helpful functions to work with arrays. We have seen many array functions in previous articles. 

One common array operation is to determine its length, which is the number of elements an array has. There are two similar functions to get array length in PHP.

In this article, we’ll help you learn these functions. First, we’ll see the syntax and then elaborate it through examples. So let’s start.

Array length in PHP: count function

Description

The count function counts all elements in an array.

Syntax

count(array $value, int $mode = COUNT_NORMAL): int

Arguments

  • $value  – An array
  • $mode  – An optional parameter with a default value of COUNT_NORMAL. We can pass COUNT_RECURSIVE (or 1) to count an array recursively. (Good for multi-dimensional arrays).

Return

  • Returns the number of elements in $value. 
  • If $value is not an array or object, the function returns 1. 
  • If $value null, the functions returns 0.

Examples

Let’s put this theory into action by examples.

<?php
//An array with many elements.
$arr = array("PHP","Javascript","Python","Java","C#","C++");
 
//Let's count this array using the count function and print it to console.
echo "Length of \$arr is: ".count($arr);
 
//OUTPUT
//Length of $arr is: 6
?>


Pretty straightforward. Remember, there’s a second parameter $mode for altering the function’s behavior. We’ll pass COUNT_RECURSIVE to it and see how it is different than the default COUNT_NORMAL.

<?php
//An array with many elements.
$arr = array("Name"=>"Robert","Age"=>"22","Skills"=>array("MongoDB","Express","React","Node.js"));
 
//Let's count this array using the count function (COUNT_NORMAL) and print it to console.
echo "Length of \$arr (Using COUNT_NORMAL) is: ".count($arr)."\n";
 
//Let's count this array using the count function (COUNT_RECURSIVE) and print it to console.
echo "Length of \$arr (Using COUNT_RECURSIVE) is: ".count($arr,COUNT_RECURSIVE);
 
//OUTPUT
//Length of $arr (Using COUNT_NORMAL) is: 3   
//Length of $arr (Using COUNT_RECURSIVE) is: 7
?>

The difference is clear, COUNT_NORMAL counts the elements in the first level, which are (Name, Age, Skills). COUNT_RECURSIVE counts the elements in the second level, which are the elements of Skills.

Using Count Function to Get Length of 2nd Level Multidimensional PHP Arrays

The COUNT_RECURSIVE constant in the count PHP function mode parameter is a great way to count all the elements in a multidimensional array. I use it all of the time whenever I need to count an unknown length set of values coming from a database or API.

Check out the following example to see the count function going beyond second-level:

Using Count for 2D PHP Array Length Code Example

<?php
//An array with many elements.
$arr = array(1,array(2,array(3,array(4))));
 
 
//Let's count this array using the count function (COUNT_RECURSIVE) and print it to console.
echo "Length of \$arr (Using COUNT_RECURSIVE) is: ".count($arr,COUNT_RECURSIVE);
 
//OUTPUT
//Length of $arr (Using COUNT_RECURSIVE) is: 7
?>


Cool. Let’s now move on to the next function: sizeOf.

Array length in PHP: sizeOf function

sizeOf function is just an alias of the count function.

Description

The sizeOf function counts all elements in an array.

Syntax

sizeOf(array $value, int $mode = COUNT_NORMAL): int

Arguments

  • $value  – An array
  • $mode  – An optional parameter with a default value of COUNT_NORMAL. We can pass COUNT_RECURSIVE (or 1) to count an array recursively. (Good for multi-dimensional arrays).

Return

  • Returns the number of elements in $value. 
  • If $value is not an array or object, the function returns 1. 
  • If $value null, the functions returns 0.

Using the SizeOf function to find the length of PHP Array Code Example

Let’s revisit the same example to get array length in PHP, using sizeOf instead.

<?php
//An array with many elements.
$arr = array("PHP","Javascript","Python","Java","C#","C++");
 
//Let's count this array using the count function and print it to console.
echo "Length of \$arr is: ".sizeOf($arr);
 
//OUTPUT
//Length of $arr is: 6
?>

sizeOf function does have the $mode parameter. Let’s alter the function to count an array recursively.

<?php
//An array with many elements.
$arr = array(1,array(2,array(3,array(4))));
 
 
//Let's count this array using the count function (COUNT_RECURSIVE) and print it to console.
echo "Length of \$arr (Using COUNT_RECURSIVE) is: ".sizeOf($arr,COUNT_RECURSIVE);
 
//OUTPUT
//Length of $arr (Using COUNT_RECURSIVE) is: 7
?>

The output is similar to the count function. You can use either of these functions to get array length in PHP.

BONUS: Common usage of array length in PHP

We do not leave a topic dangling by missing out on its practical use. Similarly, array length in PHP may have numerous uses, depends on the context you’re working. However, there is a frequently occurring practice you’ll encounter while coding.

The array length is mostly used in FOR loop to loop over an array. The idea is to terminate the loop once it reaches the end of an array, where the array length in PHP factors in.

<?php
$arr = array("First","Second","Third","Fourth","Last");
 
// Loop over the array.
for($i = 0; $i < count($arr); $i++)
{
    echo "I am the ".$arr[$i]." element"."\n";
}
 
//OUTPUT
// I am the First element 
// I am the Second element
// I am the Third element 
// I am the Fourth element
// I am the Last element  
?>

See how the count helps the loop to terminate at the right time. It also helps to get array elements by index. That’s it, we hope you’ve enjoyed the article.

Want to explore further about PHP arrays?

We have many fun articles related to PHP arrays. You can explore these to learn more about arrays in PHP.

What is PHP isset function and when to use it

Convert array to string with PHP implode function

Make an associative array in PHP

Learn the Fundamentals of Good Web Development

Please take a moment and sign up for our free email course on the fundamentals of good web development. Every week is packed with a roundup of articles on our site and from around the web, where we go deep into developing exceptional web applications. We have meetups, code reviews, slack chats, and more.

Click here to get started

Did you find this article helpful?

Join the best weekly newsletter where I deliver content on building better web applications. I curate the best tips, strategies, news & resources to help you develop highly-scalable and results-driven applications.

Build Better Web Apps

I hope you're enjoying this article.

Get the best content on building better web apps delivered to you.