PHP Array Length using count or sizeof
PHP Array Length can be measured by using either count function or sizeof function in php. both functions will work same, sizeof function is an alias of count function. By using this count function in PHP, you can measure PHP Array Length. Counts all elements in an array, or something in an object and returns numeric value. It takes two argument, first argument is the array and it is required.
PHP Array Length using count
Syntax of count PHP function
First argument is the array to count, second argument is the mode this will be used for counting multidimensional array. Set mode parameter to COUNT_RECURSIVE (or 1) for counting all the elements of a multidimensional array.
Posssible values for mode:
- 0 – Default. Does not count all elements of multidimensional arrays
- 1 – Counts the array recursively (counts all the elements of multidimensional arrays)
count ($array_or_countable , 'mode' )
Example code for finding PHP Array Length
Here in this below example code, it will counts number of pockets.
<?php $a = 1; $a = 3; $a = 5; $result = count($a); // $result == 3 ?>
Even if it has random number of keys, array count value is same
<?php $b = 7; $b = 9; $b = 11; $result = count($b); // $result == 3 ?>
If you try to count null value, output will be zero.
<?php $result = count(null); // $result == 0 ?>
If you try to count boolean value, then the output will be one.
<?php $result = count(false); // $result == 1
PHP Array Length using sizeof
Sizeof PHP function is an alias of count function, just in place of count use sizeof function. But it is recommended to use count instead of sizeof, anyway output will be same.
<?php $a = 1; $a = 3; $a = 5; $result = sizeof($a); // $result == 3 ?>
If you have any problem while using count and sizeof php functions then use below comment form let us know.
sizeof in php