PHP Array Length

In PHP, the total number of elements in an array can be calculated by couple of PHP functions count() and sizeof(), where as the sizeof() is an alias of the master function count(). We are going to see about these functions in this article in detail with suitable examples for calculating PHP array length.

count()

The syntax of PHP count() function is,

count(var array_variable, mode)

array_variable

The array variable is required for which the length will be calculated. If the value of this first argument is not set or having empty array, then count() will return 0. And, if the variable specified is not an array, then, count method will return 1. To check the state of this variable whether it is not set or it is empty, we can use PHP predefined function, isset() and empty(), respectively.

mode

This argument is optional and has value to specify the mode of operation to be used on calculating array length. There are various possible options to this argument. These are,

  • COUNT_NORMAL – By specifying this predefined constant for the optional second argument of count() function, it will return the length of an array in depth 0.
  • COUNT_RECURSIVE – This value will be used for recursive count operation, till it reaches depth n. This recursive call will be continued infinitely, till it reaches depth n or the program execution time limit is elapsed.

Note:

  • The alternative values can be passed as the value of this optional argument is, 0 and 1 to create same effect on returning array length with COUNT_NORMAL and COUNT_RECURSIVE, respectively.
  • COUNT_NORMAL is the default option, where as, if no second argument is specified while invoking count().

php_array_length

Example: PHP count()

The following program is shown as an example for count() function to calculate number of elements available in an two dimensional array. Since the input array of this program contains more than one dimensions, we can differentiate the output values return by using various mode of operations.

Let us use the same two dimensional array we have taken as an example for PHP inheritance article. That is,

$toys = array(array("name"=>"Mechanical Cars","category"=>"pull back"),
array("name"=>"Jigsaw","category"=>"puzzles"),
array("name"=>"HiTech Cars","category"=>"remote"),
array("name"=>"Teddy Bears","category"=>"soft"),
array("name"=>"Baby pillow","category"=>"soft"),
array("name"=>"Chinese Checker","category"=>"puzzles"),
array("name"=>"Jumbo Helicopter","category"=>"remote"));

Now, we can invoke count() function by passing different possible mode option, as follows,

$array_length["normal_count"] = count($toys,COUNT_NORMAL);
//(OR) 
$array_length["default_count"] = count($toys);
$array_length["recursive_count"] = count($toys,COUNT_RECURSIVE);
print "<pre>";
print_r($array_length);
print "</pre>";

In the above PHP code, the count() is invoked with normal, default and recursive mode, and the returned values are stored into an array named, $array_length, by specifying the appropriate name indices. The integer value returned as the length of the given input array will be same for the normal and default mode. This will be clear with the following output of this program.

Array
(
    [normal_count] => 7
    [default_count] => 7
    [recursive_count] => 21
)

sizeof()

This function is an alias of PHP count() function as we said at the beginning of this article, and accepts same set of arguments as like as count().

We can replace the count() with sizeof() in the above program to compare the results of both functions which will be same for both function calls.

Alias functions in PHP are preserved for providing backward compatibility, though it is not good programming practice to use alias functions. Why because, we need to maintain our code with upgraded version, since there is no guarantee for the alias functions will be exist for longer time with the future API clean up.

Particularly, sizeof() function performs differently depends on the programming languages. For example, in C, this function is used to calculate the size of data type to be represented in bytes. So, the developer who is new for PHP, migrating from C like languages, will be confused with this function. So, it is preferable to use master functions instead of aliases.

This PHP code tutorial was published on June 21, 2013.

↑ Back to Top