Skip to main content
The + operator in PHP functions as both a unary identity operator for numeric type coercion and a polymorphic binary operator for arithmetic addition and array union. Unlike languages that overload the + symbol for string concatenation, PHP strictly isolates its behavior to mathematical evaluation and array key merging.

Unary Identity Operator

When applied to a single operand, the + operator acts as the identity operator. Its primary mechanical function is to coerce scalar values into an int or float type.
Type Resolution and Coercion:
  • Numeric Types: If the operand is already an int or float, the value and type are returned unchanged.
  • Numeric Strings: If the operand is a string containing a valid numeric representation, the Zend Engine coerces it to an int or float based on the presence of a decimal or scientific notation.
  • Booleans: true is coerced to int(1) and false is coerced to int(0).
  • Invalid Types: Applying the unary + to a non-numeric string, array, or object results in a TypeError in PHP 8.0 and later.

Binary Arithmetic Addition

When evaluating two scalar values, the + operator calculates the mathematical sum of the operands.
Type Resolution and Coercion:
  • Integer + Integer: Returns an int. If the resulting sum exceeds the system’s PHP_INT_MAX threshold, the return type is automatically promoted to a float (integer overflow).
  • Float + Float / Mixed Numeric: If either operand is a float, the return type is evaluated as a float.
  • Numeric Strings: If an operand is a numeric string, PHP’s type juggling coerces it to an int or float before evaluation.

Binary Array Union

When both operands are composite array types, the + operator acts as the union operator. It appends the elements of the right-hand array into the left-hand array based on their keys.
Key Collision Resolution: During an array union, the left-hand array takes absolute precedence. If a string or integer key exists in the left operand, the corresponding key-value pair in the right operand is entirely ignored. The operator does not recursively merge nested arrays, nor does it re-index numeric keys.

Invalid Operand Combinations

Applying the binary + operator to incompatible types (e.g., attempting to add an array to an int, or an object to a float) violates PHP’s strict typing rules for this operator. In PHP 8.0+, this results in a fatal TypeError.
Tired of Poor PHP Skills? Fix That With Deep Grasping!Learn More