PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular

count

Tags: PHP 5.x PHP 7.x PHP 8.x

count — Count all elements in an array, or something in an object.

Description

Description

count(
    Countable|array $value,
int $mode = COUNT_NORMAL
): int
Counts all elements in an array, or something in an object. For objects, if you have SPL installed, you can hook into count() by implementing interface Countable. The interface has exactly one method, Countable::count(), which returns the return value for the count() function. Please see the Array section of the manual for a detailed explanation of how arrays are implemented and used in PHP.

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.

Return Values

Returns the number of elements in value. When the parameter is neither an array nor an object with implemented Countable interface, 1 will be returned. There is one exception, if value is null, 0 will be returned.

count - New snippets