{
    "slug": "php7_spaceship_operator",
    "term": "Spaceship Operator <=> (PHP 7.0)",
    "category": "php",
    "difficulty": "beginner",
    "short": "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.",
    "long": "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.",
    "aliases": [
        "combined comparison operator",
        "three-way comparison"
    ],
    "tags": [
        "php7",
        "sorting",
        "comparison",
        "version-history"
    ],
    "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"
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "type_declarations",
        "sorting_algorithms",
        "match_expression"
    ],
    "prerequisites": [
        "sorting_algorithms",
        "type_juggling"
    ],
    "refs": [
        "https://www.php.net/manual/en/migration70.new-features.php"
    ],
    "bad_code": "// PHP 5 comparison in usort:\nusort($items, function($a, $b) {\n    if ($a->price === $b->price) return 0;\n    return $a->price < $b->price ? -1 : 1;\n});",
    "good_code": "// PHP 7 spaceship operator:\nusort($items, fn($a, $b) => $a->price <=> $b->price);\n\n// Multi-key sort:\nusort($items, fn($a, $b) =>\n    [$a->category, $a->price] <=> [$b->category, $b->price]\n);",
    "quick_fix": "Replace every usort callback with return $a->field <=> $b->field; — add ?: $a->secondary <=> $b->secondary for multi-key sorts",
    "severity": "low",
    "effort": "low",
    "created": "2026-03-22",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php7_spaceship_operator",
        "html_url": "https://codeclaritylab.com/glossary/php7_spaceship_operator",
        "json_url": "https://codeclaritylab.com/glossary/php7_spaceship_operator.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Spaceship Operator <=> (PHP 7.0)](https://codeclaritylab.com/glossary/php7_spaceship_operator) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php7_spaceship_operator"
            }
        }
    }
}