How to use sizeof() Method In PHP
The sizeof() method is used to count the number of elements present in an array or any other countable object.
Syntax:
sizeof($array, mode)
Example :
<?php
// PHP program to count all elements
// or values in an array
// Use of sizeof() function
$array = array("Geek1", "Geek2",
"Geek3", "1", "2","3");
echo "Count second array elements: "
. sizeof($array) . "\n";
$array = array(
'names' => array("Geek1", "Geek2", "Geek3"),
'rank' => array('1', '2', '3')
);
echo("Recursive count: " . sizeof($array, 1) . "\n");
echo("Normal count: " . sizeof($array, 0) . "\n");
?>
Output
Count second array elements: 6 Recursive count: 8 Normal count: 2
How to count all array elements in PHP ?
We have given an array containing some array elements and the task is to count all elements of an array arr using PHP. In order to do this task, we have the following methods in PHP:
Table of Content
- Using count() Method
- Using sizeof() Method
- Using a loop
- Using iterator_count with ArrayIterator