PHP function count()

Availability in PHP 4.0

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 {

Parameters

Parameter Data type Default value Note
$var `array Countable` not
$mode int COUNT_NORMAL

Return values

int

the number of elements in var, which is typically an array, since anything else will have one element.

If var is not an array or an object with implemented Countable interface, 1 will be returned. There is one exception, if var is &null;, 0 will be returned.

Caution: count may return 0 for a variable that isn't set, but it may also return 0 for a variable that has been initialized with an empty array. Use isset to test if a variable is set.

Other resources

Official documentation for the count function

Newsletter

Nejlepsi tipy a triky o PHP do Vaseho e-mailu. Clanky a novinky nejen ze sveta PHP a programovani.