count
Counts all elements in an array or properties in an object that implements Countable.
Parameters
main.php
Return Values
int
Returns the number of elements in the array or object
On success:Number of elements as integer
On failure:TypeError if value is not countable (PHP 8.0+)
Notes
- COUNT_RECURSIVE (or 1) will recursively count multi-dimensional arrays
- Prior to PHP 8.0, non-countable values returned 1 (except null returned 0)
- Objects implementing Countable interface can be counted
- Use isset() to check if variable exists before counting
Changelog
- 8.0.0: count() will now throw a TypeError if the parameter is not countable
- 7.2.0: count() will now yield a warning on invalid countable types
Related Functions
strlen
StringReturns the length of a string in bytes.
lengthvalidationsecurity