Gen. partnerAlgotech

PHP function count()

Counts all elements in an array, or something in an object.

|
September 11, 2019

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

Loading comments...

Stay in the loop

Subscribe to our newsletter and get the latest cybersecurity news delivered straight to your inbox.

Your data is safe. You can unsubscribe from the newsletter at any time.