PHP count() Function

PHP count() Function is used to get the number of elements in an array. A count() function is built-in function in PHP.

Syntax :

count(array $array, int $mode=0);

Parameter,

$array : Required. It is input array.

$mode: Optional. Default value is 0.

   0 - Default. It does not count all elements of multidimensional arrays. It is called as COUNT_NORMAL.
   1 - It counts all the elements of multidimensional arrays.It is called as COUNT_RECURSIVE.

 

 

Return Values :

It returns numbers of elements in an array.

Let's see below example to understand php count() Function in details.

Example :

<?php
echo "<br><br> Count number of all elements in an array.";
$array= array("Games"=>array("cricket","hockey","football"), "Fruits"=> array("apple","mango"), "Colors"=>array("blue","black","white","pink"),"Food");
echo "<br>Count number of all elements in an array where mode is COUNT_NORMAL (or 0) : ".count($array);

echo "<br>Count number of all elements in an array where mode is COUNT_RECURSIVE(or 1) : ".count($array,1);

?>

Output :

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

30377