PHP Array Length
PHP Array Length
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,
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.
The alternative values can be passed as the value of this optional argument is, 0 and 1 to create the same effect on returning array length with COUNT_NORMAL and COUNT_RECURSIVE, respectively.
COUNT_NORMAL is the default option, whereas, if no second argument is specified while invoking count().
Example: PHP count()
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”),
Now, we can invoke count() function by passing different possible mode option, as follows,
$array_length[“normal_count”] = count($toys,COUNT_NORMAL);
$array_length[“default_count”] = count($toys);
$array_length[“recursive_count”] = count($toys,COUNT_RECURSIVE);
"; print_r($array_length); print "
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.
[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.
This PHP code tutorial was published on June 21, 2013.
Previous:Get Image Properties using PHPNext:PHP Predefined Error Constants
↑ Back to Top
PHPPHP IntroductionPHP BasicsPHP Shopping CartPHP Contact FormTypes, Variables & OperatorsPHP StringsPHP ArraysPHP JSON Array MergePHP ArraysPHP Array LengthPHP in_arrayPHP Array ExtractPHP Implode ExplodePHP Array SortPHP array_intersectPHP Array DifferencePHP FunctionsPHP OOPSPHP FormsAdvancedPHP AJAXRESTful APIPHP DatabasesPHP Sessions and CookiesError and Exception HandlingFiles and DirectoriesPHP Date TimePHP GraphicsPHP XMLPHP Code SamplesPHP FreelancerjQueryWordpressMySQLCSS