PHP sizeof() Function
In this chapter you will learn:
- Definition for PHP sizeof() Function
- Syntax for PHP sizeof() Function
- Parameter for PHP sizeof() Function
- Example for PHP sizeof() Function
- Example - Count the array recursively
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
Parameter | Is Required | Description |
---|---|---|
array | Required. | Specifies the array |
mode | Optional. | Specifies the mode. |
Possible values for model:
Value | Description |
---|---|
0 | Default. Does not count all elements from multidimensional arrays |
1 | Counts 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//from jav a 2 s .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.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP sort() Function
- Syntax for PHP sort() Function
- Parameter for PHP sort() Function
- Note for PHP sort() Function
- Example - PHP sort() Function
- Example - Sort the elements of the $numbers array in ascending numerical order
Home » PHP Tutorial » PHP Array Functions