> ## 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.

# PHP Subtraction

The `-` operator in PHP functions as both a binary arithmetic subtraction operator and a unary arithmetic negation operator, depending on its arity. It evaluates numeric expressions and applies PHP's standard type juggling rules to non-numeric operands.

## Binary Subtraction

When used with two operands, `-` calculates the difference by subtracting the right operand from the left operand.

```php theme={"dark"}
$result = $expression1 - $expression2;
```

**Return Type Resolution:**

* Returns an `int` if both operands evaluate to integers and the resulting value does not exceed the bounds of `PHP_INT_MAX` or `PHP_INT_MIN`.
* Returns a `float` if either operand is a float, or if the subtraction results in an integer overflow or underflow.

## Unary Negation

When used with a single operand, `-` inverts the algebraic sign of the value. Positive values become negative, and negative values become positive.

```php theme={"dark"}
$result = -$expression;
```

## Type Coercion and Strictness

PHP dynamically coerces scalar types during the evaluation of the `-` operator. In modern PHP (8.0+), strict type validation applies to non-numeric values:

* **Numeric Strings:** Automatically cast to `int` or `float` based on the string's format.
* **Booleans:** `true` is cast to `1`; `false` is cast to `0`.
* **Null:** Silently evaluates to `0`.
* **Non-Numeric Strings:** Throws a `TypeError`.
* **Arrays and Objects:** Throws a `TypeError`.

```php theme={"dark"}
// Type coercion mechanics
$a = "10" - 2;      // int(8)
$b = 5 - true;      // int(4)
$c = -false;        // int(0)
$d = 10 - null;     // int(10)

// PHP 8.0+ Error states
$e = 5 - "string";  // TypeError
$f = -[1, 2];       // 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 (`--`), and type casting operators. Unary `-` is **right-associative**, meaning chained unary operators evaluate strictly from right to left.
* **Binary `-`:** Shares precedence with addition (`+`). As of PHP 8.0, it has strictly higher precedence than string concatenation (`.`). Binary `-` is **left-associative**, meaning chained expressions are evaluated strictly from left to right.

```php theme={"dark"}
// Left-associativity of binary subtraction
$val1 = 10 - 5 - 2; 
// Evaluates as: (10 - 5) - 2 = 3

// Right-associativity of unary negation
$val2 = - -5;
// Evaluates as: -(-5) = 5

// Precedence of unary negation vs binary arithmetic
$val3 = -5 + 3;
// Evaluates as: (-5) + 3 = -2
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor PHP Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
