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

The `<=>` (spaceship) operator, formally known as the three-way comparison operator, evaluates two expressions and returns an integer representing their relative order. Introduced in PHP 7.0, it performs a combined less-than, equal-to, and greater-than comparison in a single operation.

```php theme={"dark"}
$result = $expr1 <=> $expr2;
```

## Return Value Mechanics

The operator evaluates the operands and strictly returns one of three integer values:

* `0` if `$expr1` is equal to `$expr2` (`$expr1 == $expr2`)
* `-1` if `$expr1` is less than `$expr2` (`$expr1 < $expr2`)
* `1` if `$expr1` is greater than `$expr2` (`$expr1 > $expr2`)

## Evaluation Rules and Type Coercion

The `<=>` operator relies on PHP's standard loose comparison rules. It does not perform strict type checking (like `===`); instead, it applies type coercion when comparing operands of different data types.

**Associativity:** The operator is non-associative. Chaining the operator (e.g., `$a <=> $b <=> $c`) will result in a fatal parse error.

## Syntax Visualization by Data Type

**Integers and Floats**
Numeric comparison is evaluated by standard mathematical value.

```php theme={"dark"}
echo 10 <=> 10;   // 0
echo 5 <=> 10;    // -1
echo 10 <=> 5;    // 1

echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1
```

**Strings**
Strings are evaluated using lexicographical order based on their ASCII/byte values. Case sensitivity applies, with uppercase letters having lower ASCII values than lowercase letters.

```php theme={"dark"}
echo "x" <=> "x"; // 0
echo "x" <=> "y"; // -1
echo "y" <=> "x"; // 1
echo "Z" <=> "a"; // -1 
```

**Mixed Types (Strings and Numbers)**
When comparing a string to a number, PHP's behavior depends on the string's contents and the PHP version. In PHP 8.0 and later ("Saner string to number comparisons"), if the string is non-numeric, the number is cast to a string and compared lexicographically.

```php theme={"dark"}
echo "10" <=> 10;        // 0 (Numeric string, compared as integers)
echo "5" <=> 10;         // -1 (Numeric string, compared as integers)

// PHP 8.0+ Semantics:
// 10 is cast to "10". "10 apples" is lexicographically greater than "10".
echo "10 apples" <=> 10; // 1 
```

**Booleans**
Boolean comparison treats `false` as less than `true`.

```php theme={"dark"}
echo false <=> false; // 0
echo false <=> true;  // -1
echo true <=> false;  // 1
```

**Null**
`null` evaluates as equal to `null`, `false`, and empty strings (`""`), following standard type juggling rules.

```php theme={"dark"}
echo null <=> null;   // 0
echo null <=> false;  // 0
echo null <=> true;   // -1
echo null <=> "";     // 0
```

**Arrays**
Array comparison evaluates sequentially: it first compares the count of elements, then the keys, and finally the values element by element.

```php theme={"dark"}
// Equal count and values
echo [1, 2] <=> [1, 2];       // 0

// Left array has fewer elements
echo [1, 2] <=> [1, 2, 3];    // -1

// Left array has more elements
echo [1, 2, 3] <=> [1, 2];    // 1

// Equal count, but left array has a lesser value at index 1
echo [1, 2, 3] <=> [1, 3, 3]; // -1
```

**Objects**
When comparing objects of the same class, the operator recursively compares their properties based on the order they were declared.

```php theme={"dark"}
$obj1 = (object) ['x' => 1, 'y' => 2];
$obj2 = (object) ['x' => 1, 'y' => 2];
$obj3 = (object) ['x' => 1, 'y' => 5];

echo $obj1 <=> $obj2; // 0 (Equal properties)
echo $obj1 <=> $obj3; // -1 (Property 'y' is 2 vs 5)
```

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