137 of 410 menu

The count Function

The count function counts the number of array elements.

Syntax

count(Countable|array $value, int $mode = COUNT_NORMAL): int

Example

Let's count the number of array elements:

<?php $arr = ['a', 'b', 'c', 'd', 'e']; echo count($arr); ?>

Code execution result:

5

Example

Get the last element of the array using the count function:

<?php $arr = ['a', 'b', 'c', 'd', 'e']; echo $arr[count($arr) - 1]; ?>

Code execution result:

'e'

See Also

  • the strlen function,
    which returns the string length
byenru