PHP count() Function
In this chapter you will learn:
- Definition for PHP count() Function
- Syntax for PHP count() Function
- Parameter for PHP count() Function
- Example - Count element in an array
- Example - Count the array recursively
Definition
The count() function returns the number of elements in an array.
Syntax
PHP count() Function has the following syntax.
count(array,mode);
Parameter
Parameter | Is Required | Description |
---|---|---|
array | Required. | Array to count |
mode | Optional. | Specifies the mode. |
Possible values for mode:
- 0 - Default. Does not count multidimensional arrays
- 1 - Counts the array recursively for the elements of multidimensional arrays
Example 1
Count element in an array
<?php
$cars=array("A","B","C","java2s.com");
echo count($cars);
?>
The code above generates the following result.
Example 2
Count the array recursively:
<?php// java 2 s . c o m
$a=array(
"Java"=>array("int","double"),
"CSS"=>array("height","width"),
"PHP"=>array("form")
);
echo "Normal count: " . count($a)."\n";
echo "Recursive count: " . count($a,1);
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP current() Function
- Syntax for PHP current() Function
- Parameter for PHP current() Function
- Note for PHP current() Function
- Related methods for PHP current() Function
- Example - Find out the current element
- Example - A demonstration of all related methods
Home » PHP Tutorial » PHP Array Functions