count

Counts all elements in an array or in a Countable object

&reftitle.description; `int` `count` ``Countable``array```value` `int` `mode` `COUNT_NORMAL`

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`.

&reftitle.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.

  `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.

&reftitle.returnvalues;

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.

&reftitle.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.

&reftitle.examples;

### `count` example

    <?php
    $a[0] = 1;
    $a[1] = 3;
    $a[2] = 5;
    var_dump(count($a));

    $b[0]  = 7;
    $b[5]  = 9;
    $b[10] = 11;
    var_dump(count($b));
    ?>

&example.outputs;

    int(3)
    int(3)

### `count` non Countable|array example (bad example - don't do this)

    <?php
    $b[0]  = 7;
    $b[5]  = 9;
    $b[10] = 11;
    var_dump(count($b));

    var_dump(count(null));

    var_dump(count(false));
    ?>

&example.outputs;

    int(3)
    int(0)
    int(1)

&example.outputs.72;

    int(3)

    Warning: count(): Parameter must be an array or an object that implements Countable in … on line 12
    int(0)

    Warning: count(): Parameter must be an array or an object that implements Countable in … on line 14
    int(1)

&example.outputs.8;

    int(3)

    Fatal error: Uncaught TypeError: count(): Argument #1 ($var) must be of type Countable .. on line 12

### Recursive `count` example

    <?php
    $food = array('fruits' => array('orange', 'banana', 'apple'),
                  'veggie' => array('carrot', 'collard', 'pea'));

    // recursive count
    var_dump(count($food, COUNT_RECURSIVE));

    // normal count
    var_dump(count($food));

    ?>

&example.outputs;

    int(8)
    int(2)

### Countable object

    <?php
    class CountOfMethods implements Countable
    {
        private function someMethod()
        {
        }

        public function count(): int
        {
            return count(get_class_methods($this));
        }
    }

    $obj = new CountOfMethods();
    var_dump(count($obj));
    ?>

&example.outputs;

    int(2)

&reftitle.seealso;

- `is_array`
- `isset`
- `empty`
- `strlen`
- `is_countable`
- Arrays