Skip to main content
The += operator is the addition assignment operator in PHP. It evaluates the expression on its right side, adds that value to the variable on its left side, and assigns the resulting value back to the left-side variable.
This operation is semantically equivalent to the standard assignment combined with the arithmetic addition operator:

Type-Specific Mechanics

The behavior of the += operator depends strictly on the data types of the operands involved. PHP utilizes dynamic typing and type juggling, which affects how this operator resolves. 1. Numeric Types (Integers and Floats) When both operands are numeric, standard arithmetic addition is performed. If the result of an integer addition exceeds the PHP_INT_MAX boundary, the resulting value is automatically promoted to a float.
2. Arrays When applied to arrays, += acts as the array union assignment operator. It appends elements from the right-hand array to the left-hand array. If a string or integer key exists in both arrays, the value from the left-hand array is preserved, and the right-hand value is discarded.
3. Strings and Type Coercion If the operands are strings, PHP attempts numeric type coercion prior to the addition.
  • Numeric Strings: Strings containing valid numbers (e.g., "10", "3.14") are cast to int or float before the addition is executed.
  • Non-Numeric Strings: In PHP 8.0 and later, attempting to use += with a non-numeric string throws a TypeError.

Return Value and Chaining

In PHP, assignment operations are expressions. The += operator evaluates to the final assigned value of the left operand. Because it returns a value, the operator can be chained or embedded within larger expressions.

Operator Precedence and Associativity

The += operator shares the same precedence level as the standard assignment operator (=) and other compound assignment operators (such as -=, *=, .=). It is right-associative, meaning that if multiple assignment operators appear in a single statement, the expression is evaluated and grouped from right to left.
Tired of Poor PHP Skills? Fix That With Deep Grasping!Learn More