Adding string with number in php

Adding string with number in php

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

The Hidden State of $a += $b in PHP

The discrepancy between echo and print when using the compound assignment operator (+=) on a string variable is not a bug; it is a direct consequence of how PHP handles expression evaluation, return values, and operator precedence. When you execute code like $a = "3dollars"; $b = 20;, followed by $a += $b;, the result depends entirely on whether the assignment operation returns a value to be printed or if the side effect is simply executed.

In PHP, the echo statement is strictly an expression that produces output but always returns void. When you write echo $a += $b;, PHP parses this from left to right based on operator precedence. The addition assignment operator (+=) binds tighter than the echo function call in terms of execution flow relative to the variable state, but critically, echo does not capture the return value of the expression inside it.

Let's trace the execution for $a = "3dollars" and $b = 20. When PHP encounters $a += $b, it first attempts to perform type coercion. Since $a is a string containing non-numeric characters ("3dollars"), PHP tries to cast it to an integer. The parsing stops at the first non-numeric character, so "3dollars" becomes 3. The arithmetic operation then proceeds: 3 + 20, resulting in 23. Crucially, this result is assigned back to $a. Now $a holds the integer 23.

When you immediately run echo $a += $b;, the parser evaluates the expression $a += $b first. At this specific moment, $a is 23 (integer) and $b is 20 (integer). The calculation yields 43. This new value 43 is assigned to $a. However, because echo returns nothing, you only see the state of $a before the assignment completed in the context of that specific line if logic were different, but here the confusion arises from how the expression is parsed. Actually, strictly speaking, echo $a += $b is parsed as echo ($a += $b). The expression $a += $b evaluates to 43 and assigns it to $a. Since echo prints its argument, you see 43.

Wait, let's re-examine the source material result: echo prints 23, print prints 43. This implies a different parsing or state change than standard precedence suggests. The key lies in how echo handles multiple arguments versus single arguments and the specific behavior of compound assignments when mixed with output functions.

Actually, the standard behavior for $a += $b returns the new value of $a.

  1. Line 1: echo $a += $b;

    • Expression: $a += $b.
    • Initial state: $a is "3dollars" (coerced to 3), $b is 20.
    • Calculation: 3 + 20 = 23.
    • Assignment: $a becomes 23.
    • Return value of expression: 23.
    • Output: echo prints 23.
  2. Line 2: print($a += $b);

    • Expression: $a += $b.
    • Current state of $a: It is now 23 from the previous line.
    • Calculation: 23 + 20 = 43.
    • Assignment: $a becomes 43.
    • Return value of expression: 43.
    • Output: print() prints 43.

The confusion often stems from assuming the first line resets or ignores the state. The variable $a persists across lines. After the first echo statement executes, $a holds the integer 23. When the second line runs, it adds 20 to that existing value of 23, not the original string.

This behavior highlights a critical pitfall in PHP: Variable State Mutation. Compound assignment operators like +=, -= are not just shortcuts for calculating a new value; they mutate the variable in place and return the result of that mutation. If you rely on the "original" string value for subsequent calculations without resetting, your logic will drift because the type coercion happens once per execution context or line.

In frameworks like Laravel, this distinction matters when building dynamic queries or calculating prices stored as strings (e.g., currency formatting). If you accidentally accumulate values in a variable using += while treating it as a string identifier, you might end up with unexpected integer arithmetic on coerced values. Always verify the type of your variables before performing arithmetic to ensure you aren't silently converting "100dollars" into 100 and losing the unit data.

If you need to preserve the original string content while adding a number, you must explicitly handle the conversion or separate the parsing logic from the arithmetic logic. For instance, extracting the numeric part with preg_match before performing any addition ensures that subsequent operations don't interfere with the original string representation.

Our network