1.2.718. Modernize Empty With Expression¶
empty() accepts expressions as argument. This feature was added in PHP 5.5.
There is no need to store the expression in a variable before testing, unless it is reused later.
<?php
// PHP 5.5+ empty() usage
if (empty(foo($b . $c))) {
doSomethingWithoutA();
}
// Compatible empty() usage
$a = foo($b . $c);
if (empty($a)) {
doSomethingWithoutA();
}
// $a2 is reused, storage is legit
$a2 = strtolower($b . $c);
if (empty($a2)) {
doSomething();
} else {
echo $a2;
}
?>
See also empty() and empty() supports arbitrary expressions.
1.2.718.1. Connex PHP features¶
1.2.718.1.1. Suggestions¶
Avoid the temporary variable, and use directly empty()
1.2.718.1.2. Specs¶
Short name |
Structures/ModernEmpty |
Rulesets |
|
Exakat since |
0.8.6 |
PHP Version |
With PHP 5.5 and more recent |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
High |
Available in |