Calculate PHP Array Length
Tags: php, webdev, beginners
Published: 2019-08-21 06:33:43

How to calculate the <strong>PHP array length</strong>?

If you want to count the total number of elements or values in an array, you can easily do it by using PHP inbuilt functions called <em><strong>count()</strong></em> or <em><strong>sizeof()</strong></em>.

You can find a detailed explanation with the example below that how you can calculate PHP array length.
<h2>count(): PHP Array Length For Loop</h2>
The Syntax for PHP <em><strong>count()</strong></em> function is very simple.
<pre class="EnlighterJSRAW" data-enlighter-language="null">count(var array_or_countable, mode)</pre>
<blockquote><em>If you don't know <a href="https://www.php.net/manual/en/language.types.array.php" target="_blank" rel="noopener noreferrer">what is an array</a> then please see the detailed explanation of how arrays are created and used in PHP.</em></blockquote>
<h3>array_or_countable</h3>
The first argument is an array variable that is required to calculate the PHP array length. If the first argument is empty, an empty array or not set, then <em><strong>count()</strong></em> function will return 0.

And, If the variable is specified but its data type is not an array, then, <em><strong>count()</strong></em> function will return 1.

To avoid this, you can use the PHP inbuilt function isset() and check whether an array variable is set or not.
<h3>mode</h3>
The second argument is optional and can take two values either COUNT_NORMAL  or COUNT_RECURSIVE. You can pass the value 0 and 1 instead of COUNT_NORMAL  and COUNT_RECURSIVE that will make the same effect.

The default value of the second argument is COUNT_NORMAL if no value passed as the second argument.

<strong>Example #1 count() example</strong>
<pre class="EnlighterJSRAW" data-enlighter-language="null">$food = array('fruits' =&gt; array('orange', 'banana', 'apple'),
              'veggie' =&gt; array('carrot', 'collard', 'pea'));

// recursive count
echo count($food, COUNT_RECURSIVE); // output 8

// normal count
echo count($food); or count($food, COUNT_NORMAL ); // output 2</pre>
In the above code block, We have created a multidimensional array.

When we use count function in the RECURSIVE mode as a second argument, Output is 8.

And, when we use count function in the NORMAL mode as a second argument or default mode, Output is 2.
<h2>sizeof()— PHP Array Length?</h2>
sizeof() is an alias of PHP count() function and accepts the same arguments as count().

If We replace the count() with sizeof() in the above example, the results of both functions will be the same for both functions calls.

Many programmers who come from C language background expect sizeof() to return the amount of memory allocated. But, sizeof() - as described above - is an alias for count() in PHP.

It is advised to always use count() instead of sizeof(). Because there is no guarantee for the alias function if they will exist or not in a future upgrade.

So, you should always consider to maintain code standard as well as avoid to break the application due to the upgraded version.
<h4>Want more tutorial like this? Find out <a href="https://www.techyhunger.com/" target="_blank" rel="noopener noreferrer">here</a>.</h4>
&nbsp;