PHP sizeof() | How to count the number of elements present in an array?
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.
Syntax:
Parameter
PHP size of() function accepts two parameters as shown in the syntax and parameter described below:
Name | Description | Type |
---|---|---|
array_name | This is the required parameter that represents the array containing elements that we need to count. | Array |
mode | it is an optional parameter and specifies the mode of the function. furthermore, It can take two different values as shown given below:
|
Integer |
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:
Example:1
1 2 3 4 5 6 7 8 9 10 |
<?php $s[0] = 'Social Sciences'; $s[1] = 'Physical Education'; $s[2] = 'Computer Basics'; $s[3] = 'Science'; $s[4] = 'English'; $s[5] = 'Regional Languages'; $result = sizeof($s); echo $result; ?> |
Example 2: To Count the number of elements in one dimensional array
1 2 3 4 5 6 7 8 |
<?php // input array $n=array(1,2,3,4,5,6,7,8); // getting total number of elements // present in the array. $result = sizeof($n); print($result); ?> |
Example 3: To Counting number of elements in the multi-dimensional array:
1 2 3 4 5 6 7 8 |
<?php $array = array('name' => array('Welcome', 'to', 'website', 'Tutorialscan'), 'article' => array('sizeof', 'function', 'PHP')); // recursive count echo sizeof($array, 1); // normal count echo sizeof($array); ?> |
9
2
Therefore, you can see the output result come the recursive count is 9, when normal count is 2
Conclusion:
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 info@tutorialscan.com. I will try to resolve the problem.