Q:

How to count all elements or values in an array in PHP

0

How to count all elements or values in an array in PHP

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the PHP count() or sizeof() function

You can simply use the PHP count() or sizeof() function to get the number of elements or values in an array. The count() and sizeof() function returns 0 for a variable that has been initialized with an empty array, but it may also return 0 for a variable that isn't set.

You can additionally use the isset() function to check whether a variable is set or not.

<?php
$days = array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
 
// Printing array size
echo count($days);
echo "<br>";
echo sizeof($days);
?>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP  and MySQL Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to print or echo all the values of an array in... >>
<< Counts how many times a substring occurs in a stri...