count

PHP 4, PHP 5, PHP 7, PHP 8
count - Counts all elements in an array or in a Countable object
Manual
Code Examples

count( Countable|array$value, [int$mode = COUNT_NORMAL] ): int

Counts all elements in an array when used with an array. When used with an object that implements the Countable interface, it returns the return value of the method Countable::count.

Parameters

value

An array or Countable object.

mode

If the optional mode parameter is set to COUNT_RECURSIVE (or 1), count will recursively count the array. This is particularly useful for counting all the elements of a multidimensional array.

Caution:

count can detect recursion to avoid an infinite loop, but will emit an E_WARNING every time it does (in case the array contains itself more than once) and return a count higher than may be expected.

Return Values

Returns the number of elements in value. Prior to PHP 8.0.0, if the parameter was neither an array nor an object that implements the Countable interface, 1 would be returned, unless value was null, in which case 0 would be returned.

Changelog

Version Description
8.0.0 count will now throw TypeError on invalid countable types passed to the value parameter.
7.2.0 count will now yield a warning on invalid countable types passed to the value parameter.

Related Functions

Example of count

Show all examples for count

PHP Version: