1.2.372. Don’t Mix ++¶
++ operators, pre and post, have two distinct behaviors, and should be used separately.
When mixed in a larger expression, they are difficult to read, and may lead to unwanted behaviors.
<?php
// Clear and defined behavior
$i++;
$a[$i] = $i;
// The index is also incremented, as it is used AFTP the incrementation
// With $i = 2; $a is array(3 => 3)
$a[$i] = ++$i;
// $i is actually modified twice
$i = --$i + 1;
?>
See also EXP30-C. Do not depend on the order of evaluation for side effects.
1.2.372.1. Connex PHP features¶
1.2.372.1.1. Suggestions¶
Extract the increment from the expression, and put it on a separate line.
1.2.372.1.2. Specs¶
Short name |
Structures/DontMixPlusPlus |
Rulesets |
|
Exakat since |
1.3.2 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Instant (5 mins) |
Precision |
High |
Examples |
|
Available in |