Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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.
+$operand
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.
$a = +"123";    // int(123)
$b = +"12.5";   // float(12.5)
$c = +true;     // int(1)
$d = +"1e3";    // float(1000)

Binary Arithmetic Addition

When evaluating two scalar values, the + operator calculates the mathematical sum of the operands.
$leftOperand + $rightOperand
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.
$a = 10 + 20;         // int(30)
$b = 10.5 + 2;        // float(12.5)
$c = "5" + 10;        // int(15) - String coerced to int
$d = PHP_INT_MAX + 1; // float(...) - Integer overflow promotion

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.
$leftArray + $rightArray
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.
$left  = ['a' => 1, 'b' => 2];
$right = ['b' => 99, 'c' => 3];

$result = $left + $right;
/*
array(3) {
  ["a"] => int(1)
  ["b"] => int(2)   // The left-hand value is preserved; 99 is discarded.
  ["c"] => int(3)
}
*/

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.
$invalid = ['a' => 1] + 5; 
// Fatal error: Uncaught TypeError: Unsupported operand types: array + int
Master PHP with Deep Grasping Methodology!Learn More