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:

sizeof(array_name, mode)

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:

  • 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.
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
Output: 6
Example 2: To Count the number of elements in one dimensional array

Output: 8
Example 3: To Counting number of elements in the multi-dimensional array:
Output:
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.