PHP sizeof Array Function | Return Count of Array Elements

Summary: In this article, you will learn, use of PHP sizeof array function, to Return Count of Array Elements with example. Let’s discuss this article in details.

PHP sizeof Array length

PHP program, you will use the PHP Array Length Sizeof function.
In this series of PHP tutorials, we are discussing the length of the array and how to get the actual length of the array using “Sizeof” function in PHP. As you know very well that PHP is a powerful scripting language that can be used to make more dynamic & interactive websites. furthermore, the use of array & array’s functions makes it very simple to use, the best thing is that one can do lots of manipulation using array functions. In PHP Count() and sizeof() function, both are an Identical type of functions, or in other words, solves the same purpose but “sizeof()” is just an alias of count() function.

Syntax:

integer sizeof($array, $mode = 0);

Parameters Values

The Function expects two parameters. One parameter is mandatory while the other parameter is optional. The details of the parameters is given below:

Value Description
$array: The 1st parameter is a mandatory array for which you need to count elements. Also, you are able to pass a multi-dimensional array to it.
$mode: The 2nd parameter to the sizeof() Function and is the mode. furthermore, it is an optional parameter with the default value is 0 (Zero). It can have two possible values:

  • 0 – Default. Doesn’t count all elements of multi-dimensional arrays.
  • 1 – Counts the array recursively means that the counts all elements of a multi-dimensional array.

Return Value

The PHP sizeof() Function returns the count of array elements in the array.
php sizeof array

PHP sizeof Array Example:

Program 1: Single Dimensional Basic Array

Array elements in array : 8

Simply, you can count the array elements in an array with the use of sizeof the function.

Program 2: Multi-Dimensional Array

For instance, let’s consider a multi-dimensional array input to an array using sizeof the Function.

Array elements in array : 14

You can observe the subsequent things:

.
Important Note: When the mode is 0 (default), then the function will return 7 as the total count. because it doesn’t count all the array elements recursively.
When the mode 1, then the function returns 14 as the total count. because it will count all the array elements recursively.
Program 3: Use PHP sizeof in the loops

Suppose, if you want to run through large arrays do not use sizeof() in the loops. because, instead, for better performance, then use it in the loop.

Conclusion:

In this article, you have learned How use of PHP sizeof array function, to Return Count of Array Elements with example. I hope you will enjoy it!. if have any query related program then contact me at info@tutorialscan.com I will try to resolve the problem. Stay healthy, stay safe! Happy coding! enjoy it!