> ## 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 Unary Minus

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.

```php theme={"dark"}
$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.

```php theme={"dark"}
$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`.

```php theme={"dark"}
$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.

```php theme={"dark"}
// 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)
```

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