This inbuilt function of PHP is used to count the current elements in the array. The function might return 0 for the variable that has been set to an empty array. Also for the variable which is not set the function returns 0.
Parameters: The function generally takes one parameter that is the array for which the elements is needed to be counted. But in addition, the function can take a parameter mode which tells the function to count element in which normal or recursive mode.
- $array (mandatory) : Refers to the array, whose elements are needed to be counted.
- mode (optional) : This is used to set the mode of the function. The parameter can take two possible values, either 0 or 1. 1 generally indicates to count the values of the array recursively. This helps in counting the multidimensional array. The default value is 0 or False.
Return Values: The function returns the number of elements in an array.
Below programs will help in understanding the working of count() function.
Program 1: Counting normally, that is passing mode as 0 or not passing the parameter mode.
Program 2: Counting recursively or passing mode as 1.
Recursive count: 8 Normal count: 2
- PHP | Ds\Map count() Function
- PHP Ds\Set count() Function
- PHP | SplObjectStorage count() Function
- PHP | SplDoublyLinkedList count() function
- PHP | DOMNamedNodeMap count() Function
- PHP | Ds\Deque count() Function
- PHP | SplFixedArray count() Function
- PHP | ArrayObject count() Function
- PHP | SplHeap count() Function
- PHP | Ds\Stack count() Function
- PHP | ArrayIterator count() Function
- Collect.js | count() Function
- PHP Ds\PriorityQueue count() Function
- PHP | Ds\Vector count() Function
- PHP | DOMNodeList count() Function
- PHP Ds\Queue count() Function
- PHP | SimpleXMLElement count() Function
- How to count files in a directory using PHP?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.