In PHP, the total number of elements in an array can be calculated by a 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.
The syntax of PHP count() function is,
count(var array_variable, mode)
The array variable is required for which the length will be calculated. If the value of this first argument is not set or having an 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.
This argument is optional and has value to specify the mode of operation to be used in calculating array length. There are various possible options to this argument. These are,
The following program is shown as an example for count() function to calculate a number of elements available in a 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 )
This function is an alias of PHP count() function as we said at the beginning of this article, and accepts the 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 functions 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 an upgraded version since there is no guarantee for the alias functions will exist for a 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 the 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.