← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

Spaceship Operator <=> (PHP 7.0)

PHP PHP 7.0+ Beginner
debt(d3/e1/b1/t3)
d3 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'default linter catches the common case' (d3). The detection_hints list rector and php-cs-fixer, both of which can detect the manual if/else or ternary comparison pattern in usort callbacks automatically — this is a well-known, automatable code pattern replacement.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1). The quick_fix is explicit: replace every usort callback with 'return $a->field <=> $b->field;' — a direct single-line substitution per callback, requiring no structural changes.

b1 Burden Structural debt — long-term weight of choosing wrong

Closest to 'minimal commitment' (b1). The spaceship operator is a localized syntactic choice inside sort callbacks. It imposes no persistent tax on maintainers outside the immediate comparison expression; the rest of the codebase is unaffected.

t3 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'minor surprise (one edge case)' (t3). The misconception field notes that developers often think it is only useful for sorting, missing broader three-way comparison use cases. Additionally, common_mistakes flag that PHP type juggling (without strict_types) can produce unexpected ordering. These are minor surprises but not deeply contradictory to intuition.

About DEBT scoring →

Also Known As

combined comparison operator three-way comparison

TL;DR

The spaceship operator <=> returns -1, 0, or 1 by comparing two values — it replaces verbose if/else comparison callbacks in usort() with a single concise expression.

Explanation

Before PHP 7, writing a comparison callback for usort() required an if/else structure checking equality, greater than, and less than. The spaceship operator <=> compresses this to $a <=> $b. It works on integers, floats, strings, and arrays using PHP's comparison semantics. Multiple sort keys: return $a->priority <=> $b->priority ?: $a->name <=> $b->name. Named for its resemblance to the ASCII art of a spaceship.

Common Misconception

The spaceship operator is only useful for sorting — it is useful anywhere you need a three-way comparison result, including custom ranking and tie-breaking logic.

Why It Matters

usort() callbacks using if/else for comparison are error-prone — spaceship makes the correct implementation obvious and concise.

Common Mistakes

  • Forgetting spaceship respects PHP type juggling without strict_types
  • Not chaining multiple sort keys with ?: after <=>
  • Using manual arithmetic instead of the cleaner <=> operator

Code Examples

✗ Vulnerable
// PHP 5 comparison in usort:
usort($items, function($a, $b) {
    if ($a->price === $b->price) return 0;
    return $a->price < $b->price ? -1 : 1;
});
✓ Fixed
// PHP 7 spaceship operator:
usort($items, fn($a, $b) => $a->price <=> $b->price);

// Multi-key sort:
usort($items, fn($a, $b) =>
    [$a->category, $a->price] <=> [$b->category, $b->price]
);

Added 22 Mar 2026
Edited 23 Mar 2026
Views 39
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 0 pings S 1 ping S 1 ping M 1 ping T 0 pings W 0 pings T 1 ping F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W
No pings yet today
PetalBot 1
Amazonbot 6 Google 3 ChatGPT 3 Ahrefs 3 SEMrush 3 Scrapy 3 Meta AI 2 Perplexity 2 Claude 2 Bing 1 Sogou 1 PetalBot 1
crawler 24 crawler_json 5 pre-tracking 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Replace every usort callback with return $a->field <=> $b->field; — add ?: $a->secondary <=> $b->secondary for multi-key sorts
📦 Applies To
PHP 7.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
usort with if($a==$b) return 0 if($a>$b) return 1 pattern; manual ternary comparison in sort callbacks
Auto-detectable: ✓ Yes rector php-cs-fixer
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Low ✓ Auto-fixable Fix: Low Context: Line


✓ schema.org compliant