Avoid Misreading PHP Assignment Results and Operator Precedence
Calculate PHP assignment results, group arithmetic and logical expressions, and avoid relying on evaluation order when incrementing.
To trace a PHP expression, first work out how its operators group, then identify the value assigned by each assignment. In a subtotal calculation, * groups before +, while += assigns the sum back to the subtotal. Parentheses make those steps visible. They are especially useful when a condition contains both a comparison and an assignment.
Trace the calculated value and the assignment result
This example starts with a subtotal of 10, a quantity of 3, and a unit cost of 4. It calculates a line cost, then updates the subtotal. A second subtotal starts at 10 so the two documented assignment forms can be read independently.
<?php
$subtotal = 10;
$quantity = 3;
$unitCost = 4;
$lineCost = $quantity * $unitCost; // 12
$projected = $subtotal + $quantity * $unitCost; // 22
$grouped = ($subtotal + $quantity) * $unitCost; // 52
$updatedValue = ($subtotal += $lineCost); // 22
$afterUpdate = $subtotal; // 22
$equivalentSubtotal = 10;
$assignmentResult = (
$equivalentSubtotal = $equivalentSubtotal + $lineCost
); // 22
The PHP assignment manual documents $a += $b as $a = $a + $b. Here, that means adding the line cost of 12 to the subtotal of 10 and assigning 22 to $subtotal. It also states that an assignment expression has the value assigned. That is why $updatedValue receives 22 from the parenthesized += expression. Starting separately from 10, the expanded assignment gives $equivalentSubtotal the same assigned value, and $assignmentResult receives that value too.
This distinction matters when reading a longer statement: += is not just a calculation whose answer happens to be useful later. The addition is part of an assignment, and the assignment itself produces the assigned value.
Group arithmetic before using its result
The two calculated totals above answer different questions. For $projected, multiplication groups before addition, giving 10 + (3 * 4), or 22. For $grouped, the parentheses request (10 + 3) * 4, or 52. The precedence manual illustrates the same rule with 1 + 5 * 3, which gives 16 rather than 18.
When a calculated value feeds an update, write the grouping that represents the intended calculation. Otherwise, a reader may mistake a different arithmetic result for an assignment problem. In this example, $lineCost names the 3 * 4 result before that value is used in +=.
Compare a number with numeric text, then combine conditions
Here, '12' is deliberately a numeric string. The comparison rule in the PHP comparison manual says that a number compared with a numeric string is compared numerically.
<?php
$number = 12;
$numericString = '12';
$sameNumber = ($number == $numericString); // true
$largeEnough = ($number >= 10); // true
$accepted = ($sameNumber and $largeEnough); // true
Read the two comparisons first: numerically, 12 equals 12, and 12 is at least 10. The parenthesized logical expression then has two true operands, so and produces true. Those parentheses show exactly which result is being assigned to $accepted.
Do not extend the numeric-string rule to every string. The comparison manual also notes a version distinction for other strings: before PHP 8.0.0, comparing a string with a number or numeric string converted the string to a number first, which could produce surprising results. The example above uses numeric text rather than relying on that older rule.
Put the whole logical result inside the assignment
PHP has logical operators with different precedences. Rather than guessing how an assignment mixed with and groups, put parentheses around the operation you intend to assign:
<?php
$wholeResult = (true and false); // false
$leftOnly = true;
($leftOnly = true) and false; // The assignment gives $leftOnly true
The first assignment takes the result of the complete and expression, which is false because its operands are not both true. In the second statement, only true is inside the parenthesized assignment to $leftOnly. The and false sits outside that assignment. The contrast follows from the written parentheses, without requiring a prediction about an unparenthesized assignment using and.
Finally, grouping is not a promise about the order in which every part of an expression is evaluated. The precedence manual uses this as an evaluation-order warning:
<?php
$a = 1;
echo $a + $a++; // Evaluation-order warning: do not assume one result
If an increment is needed before arithmetic, keep it in a separate statement instead of mixing both operations in one expression:
<?php
$a = 1;
$a++;
$sum = $a + $a;
That structure makes the increment and the later calculation separate steps. The supplied precedence warning does not give a dependable single result for the mixed expression, so it is not a sound place to practice predicting an assignment value.
