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 is a polymorphic operator that functions as an arithmetic addition operator for numeric types, an array union operator for arrays, and a unary identity operator for numeric casting. Its execution behavior and return type are strictly determined by the data types of the operands evaluated at runtime.

Arithmetic Addition (Binary)

When applied to numeric operands, the + operator performs standard arithmetic addition.
$result = $expression1 + $expression2;
Type Resolution and Coercion:
  • Integer + Integer: Returns an int. If the resulting value exceeds PHP_INT_MAX (integer overflow) or falls below PHP_INT_MIN (integer underflow), PHP automatically promotes the return type to a float.
  • Integer + Float (or Float + Float): Returns a float. The integer operand is implicitly widened to a float before the operation occurs.
  • Numeric Strings: If an operand is a string containing a valid numeric representation (e.g., "42" or "3.14"), PHP implicitly coerces it to an int or float.
  • Non-Numeric Strings: In PHP 8.0 and later, applying the + operator to a non-numeric string throws a TypeError. In PHP 7.1 through 7.4, it emitted a Warning and cast the string to 0. In PHP 7.0 and earlier, it performed the cast to 0 silently without emitting any warning or notice.

Array Union (Binary)

When both operands are arrays, the + operator acts as the union operator.
$result = $arrayLeft + $arrayRight;
Evaluation Mechanics:
  • The operator appends the elements of the right-hand array to the left-hand array.
  • Key Collisions: If a string or integer key exists in both arrays, the value from the left-hand array is preserved, and the corresponding key-value pair from the right-hand array is entirely ignored.
  • Index Preservation: Unlike functions such as array_merge(), the + operator does not re-index numeric keys. The original keys from both arrays are strictly maintained.

Unary Identity (Unary)

When used as a prefix to a single operand, the + operator acts as a unary identity operator.
$result = +$expression;
Evaluation Mechanics:
  • It performs an explicit type conversion, casting the operand to an int or float depending on the operand’s underlying value.
  • If the operand is already a numeric type, it returns the value unmodified.
  • If the operand is a numeric string, it resolves to the corresponding numeric type.

Strict Typing Independence

The + operator is completely unaffected by the declare(strict_types=1); directive. It consistently performs standard type juggling (such as coercing numeric strings to numbers) regardless of the strict typing context. Any TypeError associated with strict typing is triggered by function boundary type checks—either evaluating the arguments before the operator executes or evaluating the result afterward—and never by the + operator itself.
Master PHP with Deep Grasping Methodology!Learn More