There are two main inbuilt functions, count() and sizeof(), to find an array length in PHP. There are other ways to count array length in PHP, but inbuilt functions are a more straight forward and effortless approach to find array length.
Index
count() and sizeof() both functions are inbuilt in PHP in all versions. Evenmore, sizeof() function is alias of count() function. However count() function is quite popular than sizeof(), still there is no difference between both.
Syntax
count($array_name, mode);
Count() function is the most popular function to find array length in PHP. It has the feature of finding the length of an array in two different modes.
A mode is a flag/parameter in count()/sizeof() function, which indicates to count the specified array length till which depth. There are two modes available in the count() function.
Example
<?php
$arr = array("Crypto"=>array("Bitcoin", "Ethereum", "Litecoin"),"Currecy"=>array("USD", "Pound", "Rupee"));
echo "Count Default/Normal ".count($arr);
echo "<br>";
echo "Count Recursive ".count($arr, COUNT_RECURSIVE);
?>
Output
Count Default/Normal 2
Count Recursive 8
Note: COUNT_NORMAL is default parameter in count() function.
Syntax
sizeof($array_name, mode);
sizeof() function returns the exact output as count() function. It has the same mode feature with two parameters, as mentioned above.
Example
<?php
$arr = array("Crypto"=>array("Bitcoin", "Ethereum", "Litecoin"),"Currecy"=>array("USD", "Pound", "Rupee"));
echo "Count Default/Normal ".sizeof($arr);
echo "<br>";
echo "Count Recursive ".sizeof($arr, COUNT_RECURSIVE);
?>
Output
Count Default/Normal 2
Count Recursive 8
I hope now you have a complete understanding of count() and sizeof() functions in PHP to count the length of an array in PHP.
Happy Coding 🙂
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…