<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Avinash</title>
    <description>The latest articles on DEV Community by Avinash (@techyhunger).</description>
    <link>https://dev.to/techyhunger</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F213161%2F145b6383-bc1a-4051-b8d5-2f63663f3288.png</url>
      <title>DEV Community: Avinash</title>
      <link>https://dev.to/techyhunger</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/techyhunger"/>
    <language>en</language>
    <item>
      <title>Calculate PHP Array Length</title>
      <dc:creator>Avinash</dc:creator>
      <pubDate>Wed, 21 Aug 2019 06:33:43 +0000</pubDate>
      <link>https://dev.to/techyhunger/calculate-php-array-length-2elb</link>
      <guid>https://dev.to/techyhunger/calculate-php-array-length-2elb</guid>
      <description>&lt;p&gt;How to calculate the &lt;strong&gt;PHP array length&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;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 &lt;em&gt;&lt;strong&gt;count()&lt;/strong&gt;&lt;/em&gt; or &lt;em&gt;&lt;strong&gt;sizeof()&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;You can find a detailed explanation with the example below that how you can calculate PHP array length.&lt;/p&gt;

&lt;h2&gt;count(): PHP Array Length For Loop&lt;/h2&gt;

&lt;p&gt;The Syntax for PHP &lt;em&gt;&lt;strong&gt;count()&lt;/strong&gt;&lt;/em&gt; function is very simple.&lt;/p&gt;

&lt;pre class="EnlighterJSRAW"&gt;count(var array_or_countable, mode)&lt;/pre&gt;

&lt;blockquote&gt;&lt;em&gt;If you don't know &lt;a href="https://www.php.net/manual/en/language.types.array.php" rel="noopener noreferrer"&gt;what is an array&lt;/a&gt; then please see the detailed explanation of how arrays are created and used in PHP.&lt;/em&gt;&lt;/blockquote&gt;

&lt;h3&gt;array_or_countable&lt;/h3&gt;

&lt;p&gt;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 &lt;em&gt;&lt;strong&gt;count()&lt;/strong&gt;&lt;/em&gt; function will return 0.&lt;/p&gt;

&lt;p&gt;And, If the variable is specified but its data type is not an array, then, &lt;em&gt;&lt;strong&gt;count()&lt;/strong&gt;&lt;/em&gt; function will return 1.&lt;/p&gt;

&lt;p&gt;To avoid this, you can use the PHP inbuilt function isset() and check whether an array variable is set or not.&lt;/p&gt;

&lt;h3&gt;mode&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;The default value of the second argument is COUNT_NORMAL if no value passed as the second argument.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example #1 count() example&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="EnlighterJSRAW"&gt;$food = array('fruits' =&amp;gt; array('orange', 'banana', 'apple'),
              'veggie' =&amp;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&lt;/pre&gt;

&lt;p&gt;In the above code block, We have created a multidimensional array.&lt;/p&gt;

&lt;p&gt;When we use count function in the RECURSIVE mode as a second argument, Output is 8.&lt;/p&gt;

&lt;p&gt;And, when we use count function in the NORMAL mode as a second argument or default mode, Output is 2.&lt;/p&gt;

&lt;h2&gt;sizeof()— PHP Array Length?&lt;/h2&gt;

&lt;p&gt;sizeof() is an alias of PHP count() function and accepts the same arguments as count().&lt;/p&gt;

&lt;p&gt;If We replace the count() with sizeof() in the above example, the results of both functions will be the same for both functions calls.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;So, you should always consider to maintain code standard as well as avoid to break the application due to the upgraded version.&lt;/p&gt;

&lt;h4&gt;Want more tutorial like this? Find out &lt;a href="https://www.techyhunger.com/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/h4&gt;

&lt;p&gt; &lt;/p&gt;

</description>
      <category>php</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
