Latest web development tutorials

PHP sizeof () function

PHP Array Reference Complete PHP Array Reference

Examples

Returns the number of elements in the array:

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

Running instance »

Definition and Usage

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

sizeof () function is () count aliases function.


grammar

sizeof( array,mode );

参数 描述
array 必需。规定要计数的数组。
mode 可选。规定函数的模式。可能的值:
  • 0 - 默认。不计算多维数组中的所有元素。
  • 1 - 递归地计算数组中元素的数目(计算多维数组中的所有元素)。

technical details

return value: Returns the number of elements in the array.
PHP version: 4+


More examples

Example 1

Recursively compute the number of elements in the array:

<?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);
?>

Running instance »


PHP Array Reference Complete PHP Array Reference