PHP sizeof() function

P
PHP shuffle() function
PHP sort() function

In this article, you will learn how to count the array elements in PHP. The sizeof() function returns the number of elements in an array.

Note: This function is an alias of count() function.

What is the syntax of the sizeof() function in PHP?

sizeof(array, mode)
ParameterDescription
arrayThe array to count element of – Required
modeCan be either 0 or 1.
When set to 0 – Does not count elements of any subarray (multidimensional array).
When set to 1 – Count the subarray elements (multidimensional array) or count recursively.
PHP sizeof() function

Examples of sizeof() function

Example 1. Count the number of elements in an array.

<?php
$bikes=array("Suzuki","Honda","Yamaha");
echo sizeof($bikes);
?>

Example 2. Count the number of elements of the multidimensional array (recursively).

<?php
$bikes=array
  (
  "Suzuki"=>array
  (
  "CD150",
  "CD210"
  ),
  "Honda"=>array
  (
  "CD70",
  "125"
  ),
  "Yamaha"=>array
  (
  "YBR"
  )
  );

echo "count: " . sizeof($bikes)";
echo "count recursively: " . sizeof($bikes,1);
?>
PHP shuffle() function
PHP sort() function

Add comment

Tutor Network

Share this

Learn PHP from A to Z