You can use the PHP count() or sizeof() function to get the number of elements or values in an array. The count() and sizeof() method returns 0 for a variable that has been initialized with an empty array. If the array is not initialized, you will see an error. In that case, you can use the isset() function to check whether a variable is set or not.
Using sizeof():
Advertisement
1 2 3 4 5 6 7 | <?php $colors= array("green","blue","yellow","red"); // Print array size echo sizeof($colors); ?> |
Using count():
1 2 3 4 5 6 7 8 | <?php $colors= array("green","blue","yellow","red"); // Print array size echo count($colors); ?> |