NOVINKA: Najžiadanejšie rekvalifikačné kurzy teraz s 50% zľavou + kurz AI ZADARMO. Nečakaj, táto ponuka dlho nevydrží! Zisti viac:

PHP - count

Trieda

Koreň \ Bez triedy

Metóda - count

(PHP 4, PHP 5, PHP 7)

Counts elements in an array or in an object.

To count objects, the Countable interface needs to be implemented.

Procedurálne

  • function count (mixed $array_or_countable, int $mode = COUNT_NORMAL) : int

Parametre

NázovDátový typPredvolená hodnotaPopis
$array_or_countablemixed

An array or an object implementing the Countable interface.

$modeint COUNT_NORMAL

We can set the COUNT_RECURSIVE constant as the second parameter. In that case, the function returns the number of elements even in all subarrays, so we can get the number of elements in multidimensional arrays.

Mávratovej hodnoty

Vracia: int

The number of elements. If an array or a valid object with implemented Countable isn't set, it returns the value of 1. If it is NULL, it returns 0. Caution: the count() function returns 0 even for uninitialized variables.

Príklady

<?php
$array = [1, 2, 3, 4, 5, 6];
echo count($array);

Example with the COUNT_RECURSIVE parameter:

<?php
$array = [1, [4, 7, 11], 2, 3, 4];
echo count($array, COUNT_RECURSIVE);

Súvisiace manuály

      • function empty (mixed $var) : bool
      • function isset (mixed $var, mixed $...) : bool
      • function is_array (mixed $var) : bool
      • function strlen (string $string) : int
      Aktivity