TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
- 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.
- Returns an
intif both operands evaluate to integers and the resulting value does not exceed the platform’s integer bounds (PHP_INT_MAXorPHP_INT_MIN). - Returns a
floatif 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.
- The operation generally preserves the numeric type (
intorfloat) of the operand. - Exception: Negating the minimum representable integer (
PHP_INT_MIN) results in afloat. Because the absolute value ofPHP_INT_MINis one greater thanPHP_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 correspondingintorfloatvalues 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 aTypeError. - Booleans:
trueis coerced to the integer1, andfalseis coerced to the integer0. - Null:
nullis coerced to the integer0. - Arrays and Objects: Attempting to use the
-operator on arrays or standard objects throws aTypeError.
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.
Master PHP with Deep Grasping Methodology!Learn More





