Skip to main content
The - operator in PHP functions as both a binary arithmetic operator for subtraction and a unary arithmetic operator for arithmetic negation, depending on its arity within an expression.

Binary Subtraction

When used with two operands, the - operator calculates the difference between the left operand and the right operand.
Type Resolution:
  • Returns an int if both operands evaluate to integers and the resulting value does not exceed the platform’s integer bounds (PHP_INT_MAX or PHP_INT_MIN).
  • Returns a float if either operand is a float, or if the resulting integer difference causes an overflow or underflow.

Unary Negation

When prepended to a single operand, the - operator acts as a unary operator that inverts the algebraic sign of the value.
Type Resolution:
  • The operation generally preserves the numeric type (int or float) of the operand.
  • Exception: Negating the minimum representable integer (PHP_INT_MIN) results in a float. Because the absolute value of PHP_INT_MIN is one greater than PHP_INT_MAX, the operation causes an integer overflow.

Type Coercion and Juggling

PHP enforces strict type evaluation rules when applying the - operator to non-numeric scalar types:
  • Numeric Strings: Strings containing valid numeric representations (e.g., "42", "-3.14", "1.2e3") are automatically cast to their corresponding int or float values before the operation.
  • Non-Numeric Strings: In PHP 8.0 and later, applying the - operator to a non-numeric string or a string with trailing non-numeric characters (e.g., "10 apples") throws a TypeError.
  • Booleans: true is coerced to the integer 1, and false is coerced to the integer 0.
  • Null: null is coerced to the integer 0.
  • Arrays and Objects: Attempting to use the - operator on arrays or standard objects throws a TypeError.

Precedence and Associativity

The PHP parser handles the - operator differently based on its context:
  • Unary -: Shares the exact same precedence level as increment/decrement operators (++, --) and type casts (e.g., (int), (float)). It evaluates with right-to-left associativity.
  • Binary -: Shares precedence with addition (+). It sits below multiplication, division, and modulo (*, /, %), and, as of PHP 8.0, sits strictly above the string concatenation operator (.). It evaluates with left-to-right associativity.
Tired of Poor PHP Skills? Fix That With Deep Grasping!Learn More