Home >>PHP Array Functions >PHP count() Function

PHP count() Function

PHP count() Function

PHP count() function is used to count the number of elements present in the given array. It accepts two parameters out of which only one is mandatory. It will return the number of elements in the array as output and zero in the case of an empty array.

Syntax:

count($array, $mode);

Parameter Values

Parameter Description
array This is a required parameter. This parameter the array.
mode This is an optional parameter. This parameter the mode.

Here is an example of count() function in PHP:

<html>
<body>
<?php
$x=array("a","b","c","d","e","f","g","h","i","j");
echo count($x);
?>
</body>
</html>
Output:
10 

Example 2:

<html>
<body>
<?php
$cars=array("a"=>array("1","2"),"b"=>array("3","4"),"c"=>array("5")); 
echo "Normal count: " . count($cars)."<br>";
echo "Recursive count: " . count($cars,1);
?>
</body>
</html>
Output:
Normal count: 3
Recursive count: 8	

No Sidebar ads