note 113617 deleted from function.count by cmb
| From: | cmb@php.net | Date: | Tue, 04 Oct 2016 18:13:38 +0000 |
| Subject: | note 113617 deleted from function.count by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-206771@lists.php.net to get a copy of this message | ||
Note Submitter: MaxTheDragon at home dot nl
----
If you wish to test if an array contains any elements (or is empty), you might tempted to use count
to do something like this:
<?php
if (count($my_array) > 0) {
// array is not empty...
}
// or this ...
if (count($my_array) === 0) {
// array is empty...
}
?>
Using the count function here is totally unnecessary and slower.
Instead, you should rely on the fact that empty arrays evaluate to false when cast to a boolean:
<?php
$hasElements = (boolean) $my_array;
// or ...
$isEmpty = !$my_array;
// or in a conditional statement...
if ($my_array) {
// array is not empty...
}
?>
Only use count if you need to know if an array contains a specific number of elements n (where n
> 0).