{
    "slug": "spread_operator",
    "term": "Spread Operator (...)",
    "category": "php",
    "difficulty": "intermediate",
    "short": "Unpacks arrays or traversables into function argument lists or array literals; also used for variadic parameters.",
    "long": "PHP's spread operator (...) serves two purposes: in function signatures it collects remaining arguments into an array (variadic), and in call sites or array literals it unpacks an iterable into individual elements. Since PHP 8.1, string-keyed arrays can be spread in function calls (named arguments). This enables array merging ($merged = [...$a, ...$b]) as a readable alternative to array_merge(), and passing stored argument lists to functions.",
    "aliases": [
        "splat operator",
        "... operator PHP",
        "argument unpacking"
    ],
    "tags": [
        "php",
        "syntax",
        "arrays",
        "functions"
    ],
    "misconception": "The spread operator can only be used with indexed arrays. Since PHP 8.1 the spread operator supports string-keyed arrays in function calls and array expressions — earlier versions throw a fatal error, so check the PHP version before relying on this.",
    "why_it_matters": "The spread operator (...) unpacks arrays into function arguments or array literals — eliminating call_user_func_array() boilerplate and enabling clean array merging with override semantics.",
    "common_mistakes": [
        "Spreading a non-array or non-Traversable — causes a TypeError at runtime.",
        "Using spread for array merging when array_merge() semantics (re-indexing numerics, last-write-wins for strings) are needed.",
        "Spreading associative arrays in PHP 7 — string key spread was only added in PHP 8.1.",
        "Not knowing spread works in array literals: $merged = [...$defaults, ...$overrides]."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "named_arguments",
        "array_functions"
    ],
    "prerequisites": [
        "variadic_functions",
        "array_functions",
        "named_arguments"
    ],
    "refs": [
        "https://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list"
    ],
    "bad_code": "// Old verbose approach:\n$args = [1, 2, 3];\ncall_user_func_array('array_merge', $arrays); // Replaced by spread\n\n// With spread operator:\nfunction sum(int ...$nums): int { return array_sum($nums); }\n$numbers = [1, 2, 3];\necho sum(...$numbers); // Clean argument unpacking",
    "good_code": "// Spread in function calls\nfunction add(int $a, int $b, int $c): int { return $a + $b + $c; }\n$args = [1, 2, 3];\necho add(...$args); // 6\n\n// Spread in array literals\n$first  = [1, 2, 3];\n$second = [4, 5, 6];\n$merged = [...$first, ...$second]; // [1,2,3,4,5,6]\n\n// PHP 8.1 — spread with string keys (named arguments style)\n$defaults = ['color' => 'blue', 'size' => 'M'];\n$options  = ['size' => 'XL', ...$defaults]; // ['size' => 'M', 'color' => 'blue']\n// Note: later keys win — order matters\n\n// Variadic parameters\nfunction sum(int ...$nums): int { return array_sum($nums); }\nsum(1, 2, 3); // 6",
    "quick_fix": "Use the spread operator (...) to unpack arrays as function arguments and merge arrays — it replaced call_user_func_array() and is cleaner than array_merge() for combining indexed arrays",
    "severity": "low",
    "effort": "low",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/spread_operator",
        "html_url": "https://codeclaritylab.com/glossary/spread_operator",
        "json_url": "https://codeclaritylab.com/glossary/spread_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": "[Spread Operator (...)](https://codeclaritylab.com/glossary/spread_operator) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/spread_operator"
            }
        }
    }
}