count
Returns the number of elements in an array.
Available since: PHP 4
Syntax
count(Countable|array $value, int $mode = COUNT_NORMAL): intParameters
| Parameter | Type | Description |
|---|---|---|
| $value | Countable|array | An array or an object implementing the Countable interface. |
| $mode | int | With `COUNT_RECURSIVE`, elements of nested arrays are counted too. |
Return value
intThe number of elements.
Examples
A regular and an empty array:
<?php
echo count(['a', 'b', 'c']), "\n";
echo count([]);Output:
3
0Checking that an array is not empty:
<?php
$items = ['tea'];
var_dump(count($items) > 0);Output:
bool(true)Notes
Passing something that is neither an array nor a Countable object — a string or null, for example — throws a type error.
See also
Related recipes
- How to check if an array is empty
- How to count occurrences of a value in an array
- How to count words in a string
- How to delete a row from a database
- How to find the average of an array
- How to find the median of an array
- How to get the first and last element of an array
- How to loop through an array with an index
- How to paginate an array
- How to sum the values of an array
In the tutorial
- Arrays — Lists and associative arrays in PHP — creating, changing, iterating and useful functions.