How to find Array Length in PHP?

PHP is a popular general-purpose scripting languages, designed to create dynamic and interactive websites, mostly used for web development. 

PHP also has powerful data structures that make PHP programming highly efficient among its wide range of functionalities. 

One such data structure is ARRAYS. 

By general definition, an array is a collection of similar data files stored at adjoining memory locations. The length property of an array is 32-bit unsigned integer that is always numerically bigger than the highest index in the array.

In this article, we’ll dive into arrays and give you a quick overview of how you can find the size of PHP arrays.

To put it simply, you can use the PHP count() or sizeof() function to find out the PHP array count or a specific number of elements in an array. They are built-in functions in PHP that return the number or size of elements in their respective arrays.

The count function is used for counting the elements or properties of an array.

What is an Array?

In PHP, an array is a data structure that allows you to store multiple elements in a single variable, stored as key-value pairs. 

While programming, you need to store humongous amounts of data of similar type, which requires defining numerous variables. It is next to impossible to remember all the variables while programming. That is where an array comes in handy. You can define an array and store all the variables in it. In practice, all elements in an array have similar data types. This data can be accessed directly only by using its index number. 

Whenever you need to store a list of elements, using an array is an intelligent decision. There are several types of arrays and different ways to initialize an array. 

Types of Arrays

Typically, arrays can be classified as:

1. Numerically Indexed arrays 

The function of indexed arrays is to store a series of one or more values. You can look up data by using their position in their respective arrays. The first index is always the digit zero, and it increases by one for each subsequent element that is added to the array. You can create an indexed array by initializing it with an array literal or calling the array class constructor.

2. Multidimensional arrays

In ActionScript, arrays can be nested as arrays that are arrays of arrays. These are known as multidimensional arrays. While programming, people often use multidimensional arrays while programming complicated structures. Each element consists of an indexed array that stores the list of tasks. To retrieve elements from a multidimensional array, you have to use multiple array access operators after the name of the top-level array.

3. Associative arrays

An associative array is like an object. Instead of a numeric index, it is made of unordered keys and values to organize and store data. Each key has a unique value and is associated with one value. That is what you need to use to access that particular value. That value can be any type of data, including a number, array, object, etc. While using associative arrays, it is helpful to remember it by a single string rather than a number on the index.

How to find array length in PHP?

Array length is the number of elements that an array can hold. Using a loop to count the array length is usually a waste of time. The two most popular ways of finding array length in PHP are the count() and sizeof() function. 

To find the length of an array, you should use the array data member’ length’. That gives you the number of elements allocated and not the number of elements inserted. You have to write a class with the main method that creates an array of 10 integers and adds them up. 

Arrays specify the array variable required to calculate the PHP array length; specifying the mode is optional. The possible values can either be zero or one, which represents COUNT_NORMAL and COUNT_RECURSIVE. If no value passes the second argument, then the default value becomes COUNT-NORMAL. This mode does not count all the elements of a multidimensional array.

The count() function and the sizeof() function are the same. They operate on similar instructions and return the same number of elements in an array. 

But for these functions to work accurately, you have to know to write commands in the correct syntax. For example, the sizeof() functions accept two parameters: 

  • Array – this parameter consists of the array containing the number of elements we need to count. 
  • Mode – this is an optional parameter that can take the two values 0 and 1. 0 is a default that does not count all variables of a multidimensional array, and 1 counts the array recursively. 

Therefore, the appropriate syntax for this command would be: 

int sizeof(array, mode);

This function returns an integer value as shown in the syntax which represents the number of elements present in the array. 


$months = array(“January”, February”, March”, “April”);

// Printing array size

echo count($months)

echo “<br>”;

echo sizeof($months);


Useful Array Functions

  • The count() function is used to count the number of elements in an array. This is also helpful when you want to recapitulate an array with a loop.
  • The is_array function is one of the most useful functions since it checks if a variable is an array or some other kind of data.
  • The in_array function helps you check an element, and the second argument is the array itself.
  • The explode function helps in distributing a string in several bits and returning it as an array.
  • The implode function performs exactly the opposite of what explode function does. It takes a series of arrays and glues them together as one string. 
  • The array_push function is used to add new elements to the end of an array.
  • The array_pop functions help in removing the last element from the end of an array.

Upskill with upGrad

If you would like to learn more about how arrays work and PHP. In that case, we recommend upGrad’s Master of Science in Data Science from Liverpool John Moores University and IIIT Bangalore, which is an 18-month online course that includes three functional specializations. The program includes best-in-class content, 500+ hours of learning material, 25 1:8 coaching sessions, and 20+ live sessions. 

Students benefit from numerous collaborative opportunities with access to upGrad’s 40,000+ learning base. 

So, what are you waiting for? Book your seat today!

Learn data science courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

What is PHP and why is it used?

PHP is a server-side language, which has been in existence for almost 25 years. Hypertext Preprocessor (PHP) is a general-purpose scripting language that is regularly used to create interactive websites. It was one of those first languages that could be inserted into HTML, making web pages more functionally involving and user-friendly.

What is the difference between the sizeof() and count() functions?

As per the official documentation of PHP, there is no difference between these two functions. They offer identical results to similar commands. The main difference lies in the method of detecting the number of variables in a particular array. But it is wiser to stick to the count() function since other programmers may expect the sizeof() function to return the size of a particular array in bytes or memory.

Is there any restriction on array length in PHP?

There is technically no maximum limit for an array. There is a default restriction on the script’s memory that you can use, but that can be changed from the “memory_limit” in your php.ini configuration. But if your arrays get bigger than the intended capacity, your system will show an “out of memory” error.

Want to share this article?

Preparer for a Career of future

Leave a comment

Your email address will not be published. Required fields are marked *

Our Best Data Science Courses

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *