PHP Array count() Function
The count( ) function is an inbuilt function of PHP, and it is used to count the elements of an array or the properties of an object. This function was introduced in PHP 4.0.
Syntax
int count ( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] );
Parameters
Parameter | Description | Is Compulsory |
---|---|---|
array | The input array. | Compulsory |
Mode | The specification can take two possible values, either 0 or 1. One generally indicates to count the values of the array recursively. This helps in counting the multidimensional array. The default value is 0 or False. | Optional |
Return
The count( ) function returns the number of elements in an array
PHP Example
<?php
$array = array("javatpoint", "udemy", "udacity", "coursera", "edureka");
print_r(count($array));
?>
Output
5
PHP Example
<?php
$fruits=array("Banana","Cherry","Apple");
echo count($fruits);
?>
Output
3
PHP Example
<?php
$days = array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
echo count($days);
?>
Output
7
PHP Example
<?php
$a[0] = 'OS';
$a[1] = 'compiler';
$a[2] = 'DBMS';
$a[3] = 'SE';
$result = count($a);
echo $result;
?>
Output
4