Summary: in this article, you will learn How to used PHP sizeof() function to count the number of elements present in an array? Let’s understand the PHP sizeof() function with example.
Answer: Used PHP sizeof() function to Count all elements in an array
The sizeof() function is a built-in function in PHP and it is an alias of count() function. which is used to count the number of elements present in an array, or properties in an object. It returns the number of elements in an array.
PHP size of() function accepts two parameters as shown in the syntax and parameter described below:
||This is the required parameter that represents the array containing elements that we need to count.
||it is an optional parameter and specifies the mode of the function. furthermore, It can take two different values as shown given below:
- 0: It is the default, doesn’t count all elements of multidimensional arrays
- 1: It Counts the array recursively which means that it counts all the elements of multidimensional arrays.
Return Value: This function will return an integer value as shown in the syntax that represents the number of elements present in the array.
PHP sizeof() Function Example:
$s = 'Social Sciences';
$s = 'Physical Education';
$s = 'Computer Basics';
$s = 'Science';
$s = 'English';
$s = 'Regional Languages';
$result = sizeof($s);
Example 2: To Count the number of elements in one dimensional array
// input array
// getting total number of elements
// present in the array.
$result = sizeof($n);
Example 3: To Counting number of elements in the multi-dimensional array:
$array = array('name' => array('Welcome', 'to', 'website', 'Tutorialscan'),
'article' => array('sizeof', 'function', 'PHP'));
// recursive count
echo sizeof($array, 1);
// normal count
Therefore, you can see the output result come the recursive count is 9, when normal count is 2
In this article, you have learned that How to used PHP sizeof() function to count the number of elements present in an array with the different example. I hope you will enjoy it!. if have any query then contact on email@example.com. I will try to resolve the problem.