PHP count() Function

You can use the PHP count() or sizeof() function to get the number of elements or values in an array. The count() and sizeof() function returns 0 for a variable that has been initialized with an empty array, but it may also return 0 for a variable that isn’t set.

Parameter Description
array Required. Specifies the array
mode Optional. Specifies the mode. Possible values:

  • 0 – Default. Does not count all elements of multidimensional arrays
  • 1 – Counts the array recursively (counts all the elements of multidimensional arrays)

Syntax

 count(array,mode); 

Example

$names=array("Samuel","Lachlan","Alex");
echo count($names);

Output

3

Leave a Reply

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