1.2.798. No Count With 0¶
Comparing count(), strlen() or mb_strlen() to 0 is a waste of resources. There are three distinct situations.
When comparing count() with 0, with ===, ==, !==, !=, it is more efficient to use empty(). empty() is a language construct that checks if a value is present, while count() actually load the number of element.
When comparing count() strictly with 0 and >
, it is more efficient to use !(empty( ))
Comparing count(), strlen() or mb_strlen() with other values than 0 cannot be replaced with a comparison with empty().
Note that this is a micro-optimisation : since PHP keeps track of the number of elements in arrays (or number of chars in strings), the total computing time of both operations is often lower than a ms. However, both functions tends to be heavily used, and may even be used inside loops.
<?php
// Checking if an array is empty
if (count($array) == 0) {
// doSomething();
}
// This may be replaced with
if (empty($array)) {
// doSomething();
}
?>
See also count, strlen and mb_strlen.
1.2.798.1. Connex PHP features¶
1.2.798.1.1. Suggestions¶
Use empty() on the data
Compare the variable with a default value, such as an empty array
1.2.798.1.2. Specs¶
Short name |
Performances/NotCountNull |
Rulesets |
|
Exakat since |
0.8.4 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Instant (5 mins) |
Precision |
Very high |
Examples |
|
Available in |