count()
21st January 2021
0 Comments
Programming Language
PHP [Versions: 4, 5, 7, 8]
Syntax
Typical Usage
Getting the number of elements in a PHP array.
Accepted Parameters
- array (array) (Required): The array you wish to to count. May also be something in an object.
- mode (integer) (Optional) (Default = COUNT_NORMAL): If set to 1 or COUNT_RECURSIVE, you can get the count all the elements of a multi-dimensional array.
Return Value(s)
Returns an integer with the amount of elements in the array.Notes
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.
Examples
Example 1
<?php
$cars = ['focus', 'fiesta', 'kuga', 'mustang'];
print_r( count($cars) );
The result is 4. Four elements in the array.
Used In Projects
Similar Functions in Other Languages
- Javascript: str.length
Was This Helpful?
If this post has helped you, please let me know. Your feedback will be highly appreciated and will help me build better content.