1.2.21. Adding Zero¶
Adding 0 is useless, as 0 is the neutral element for addition. Besides, when one of the operands is an integer, PHP silently triggers a cast to integer for the other operand.
This rule also report using +
with variables, proeprties, etc. which triggers an automated conversion to integer.
It is recommended to make the cast explicit with (int)
.
<?php
// Explicit cast
$a = (int) foo();
// Useless addition
$a = foo() + 0;
$a = 0 + foo();
// Also works with minus
$b = 0 - $c; // drop the 0, but keep the minus
$b = $c - 0; // drop the 0 and the minus
$a += 0;
$a -= 0;
$z = '12';
print +$z + 1;
?>
1.2.21.1. Connex PHP features¶
1.2.21.1.1. Suggestions¶
Remove the +/- 0, may be the whole assignation
Use an explicit type casting operator (int)
1.2.21.1.2. Specs¶
Short name |
Structures/AddZero |
Rulesets |
|
Exakat since |
0.8.4 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Instant (5 mins) |
Precision |
High |
ClearPHP |
|
Examples |
|
Available in |