The count Function

The count function counts the number of array elements.

Syntax

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

Example

Let's count the number of array elements:

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

Code execution result:

php5

Example

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

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

Code execution result:

php'e'

See Also

  • the strlen function,
    which returns the string length