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 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.
$result = $expression1 - $expression2;
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.
$result = -$expression;
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.
$a = 10 - "5";       // int(5)
$b = 10 - true;      // int(9)
$c = 10 - null;      // int(10)
$d = -"3.14";        // float(-3.14)
$e = -PHP_INT_MIN;   // float(...) due to overflow

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.
// Left-to-right associativity for binary subtraction
$result = 10 - 5 - 2; // Evaluates as (10 - 5) - 2, resulting in int(3)

// Unary precedence
$result = 5 * -3;     // Evaluates as 5 * (-3), resulting in int(-15)
Master PHP with Deep Grasping Methodology!Learn More