PHP Array Length | PHP COUNT | How to get array length in PHP

PHP Array Length
PHP Array Length: In this article, we will use the PHP count() or sizeof() function, to calculate the PHP Array length or the number of elements or value in an array.
Therefore, To count all of the elements in a PHP array, you can either use the count() function or the sizeof() function, which returns 0 for a variable that has been initialized with an empty array, furthermore, it may also return 0 for a variable that isn’t set. therefore, to check whether a variable is set or not, you can use the isset() function.


The Answer will be: 10
furthermore, run the above code segment, then you will find that the output of count() is “10” or output of sizeof() is “10”. therefore, This is because there are ten elements in the $months array.
Let us see in the detailed explanation with the example as given below that how you can calculate the PHP array length.

count(): PHP Array Length For Loop

The Syntax for PHP count() function is:

Syntax: count(var array_or_countable, mode)

Explanation: array_or_countable

The first argument is an array variable that’s needed to calculate the PHP array length. furthermore, If the first argument is empty, an empty array or not set, then count() the function will return 0. Therefore, if the variable is specified but its data type is not an array, then, the count() function will return 1.
Furthermore, to avoid this, you’ll be able to use the PHP inbuilt function isset() and check whether an array variable is set or not.


The second argument is optional and it can take two values either COUNT_NORMAL or COUNT_RECURSIVE therefore, you can pass the value 0 and 1 instead of COUNT_NORMAL and COUNT_RECURSIVE and it will make the same effect.
Furthermore, the default value of the second argument is, COUNT_NORMAL when there is no value passed as the second argument.

The Answer will be: 4

In the given below example, I will show you the difference between different PHP array size counting modes. Therefore, it $fruits is a multidimensional array that contains three (3) arrays that have seven elements between them. furthermore, Using different modes fetches us different PHP array sizes:

A normal count is: 3
The multidimensional count is: 10

sizeof(): PHP Array Length

Therefore, the sizeof() is an alias of PHP count() function and it accepts the same arguments as count().
furthermore, If you want to replace the count() with sizeof() then the results of both functions will be the same for both functions calls. as a personal view, advised to you that always use count() instead of sizeof(). the reason is that there is no guarantee for the alias function Therefore if they will exist or not in a future upgrade.
So, you should always consider to maintain code standard as well as avoid to break the application due to the upgraded version.

Example: To Count the number of elements in a PHP array

Here, you don’t need to use a loop to count the number of elements in the array, because as this would be extremely wasteful.
Therefore, To count the number of elements in an array, you need to use PHP’s native count function such as:

The Answer will be: 7
If you will run the above code segment, then you will find that the output of count() is “7”. therefore, This is because there are seven elements in the $country array.

The Answer will be: 3

Example: To Count elements in a Multidimensional Array.

In these cases, you just need to count all of the elements in a PHP multidimensional array. To do this, you may need to use the count function’s second parameter, which is named mode. furthermore, to achieve this, you can simply pass the constant COUNT_RECURSIVE in as the second parameter:

The Answer will be: 9

Note: Therefore, the above-given code segment will print out “9” instead of “8”. therefore, because of the array containing 25, 32, 90, and 85 is also considered to be the element.

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

The count() function and the sizeof() the function is the same and does the exact same thing. Therefore, In fact, sizeof is merely an alias of the count() function.
Furthermore, as personally, I will suggest to you that you stick to using the count function because other programmers may expect the sizeof() function to return the size of the array in bytes/memory.
Note that as of PHP 7.2, the count function will emit an E_WARNING error, therefore, if you provide it with a variable that isn’t an array or a Countable object.


Therefore, as you have seen the above, how to get the length of PHP or the number of elements or value in an array with the use the PHP count() or sizeof() function. if you have any question regarding this topic, drop me a mail, I will contact you. I hope you will enjoy these articles.