Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

sizeof()

Example

Return the count of elements in an array.

<?php
$cars=array(“Volvo”,“BMW”,“Toyota”);
echo sizeof($cars);
?>

Definition and Usage

The sizeof() function returns the number of elements in an array.

It is an alias for the count() function.

Syntax

sizeof(array, mode)

Parameter Values

 

Parameter

Description

array

Required. Specifies the array to be counted.

mode

Optional. Specifies the mode. Possible values are:

  • 0 – Default. Does not count elements in multidimensional arrays
  • 1 – Counts all elements recursively, including those in multidimensional arrays

 

Technical Details

Return Value:

Returns the total count of elements in the array.

PHP Version:

4+

More Examples

Example

Count all elements in the array, including those in nested arrays.

<?php
$cars=array
  (
  “Volvo”=>array
  (
  “XC60”,
  “XC90”
  ),
  “BMW”=>array
  (
  “X3”,
  “X5”
  ),
  “Toyota”=>array
  (
  “Highlander”
  )
  );

echo “Normal count: “ . sizeof($cars).“<br>”;
echo “Recursive count: “ . sizeof($cars,1);
?>