Home > Article > Backend Development > How to use the PHP function count() to find the length of an array_PHP Tutorial
New Learningint count (mixed var)
Returns the number of cells in var, usually an array (any other type has only one cell).
If var is not an array type, 1 will be returned (exception: the result of count(NULL) is 0).
Warning
The PHP function count() returns 0 for uninitialized variables, but also returns 0 for empty arrays. Use isset() to test whether a variable has been initialized.
Example 1. count() example
<ol class="dp-xml"><li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>$vegetables[0] = "corn"; </SPAN><LI class=alt><SPAN>$vegetables[1] = "broccoli"; </SPAN><LI class=""><SPAN>$vegetables[2] = "zucchini"; </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>text</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>implode</FONT></SPAN><SPAN>(",", $vegetables); </SPAN></SPAN><LI class=""><SPAN>echo $text; </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span><span></span> </li></ol>
Sometimes if you don’t know the PHP function count(), use the stupid method: select count(*) from table