PHP sizeof() Function

Definition

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

The sizeof() function is an alias of the count() function.

Syntax

PHP sizeof() Function has the following syntax.

sizeof(array,mode);

Parameter

ParameterIs RequiredDescription
arrayRequired.Specifies the array
modeOptional.Specifies the mode.

Possible values for model:

ValueDescription
0Default. Does not count all elements from multidimensional arrays
1Counts the array recursively for all the elements of multidimensional arrays

Example 1


<?php
$cars=array("A","B","C");
echo sizeof($cars);
?>

The code above generates the following result.

Example 2

Count the array recursively:


<?php/*  w w  w. j a  va2s. c  o  m*/
$cars=array("A"=>array("A1","A2"),
            "B"=>array("B1","B2"),
            "C"=>array("C1")
); 

echo "Normal count: " . sizeof($cars)."\n";
echo "Recursive count: " . sizeof($cars,1);
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions