--- id: php-functions title: "PHP Functions" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["variadic functions", "strict_types", "PHP ν•¨μˆ˜"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.9 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["php", "programming", "w3schools", "functions"] raw_sources: ["https://www.w3schools.com/php/php_functions.asp"] applied_in: [] github_commit: "" --- # [[PHP Functions]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) The `...` variadic operator can ONLY be the LAST parameter β€” putting it first (`function myFamily(...$firstname, $lastname)`) is explicitly shown as an ERROR case, because a variadic parameter greedily collects all remaining arguments into an array, leaving nothing for any parameter declared after it. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **1000+ built-in functions** available directly. [S1] - **User-defined function** β€” `function name($params) { ...; return $value; }`; not case-sensitive; not auto-run (must be called). [S1] - **Default parameter values** β€” `function setHeight($height = 50) {...}` β€” used when the argument is omitted. [S1] - **Pass by reference** β€” `function add_five(&$value) { $value += 5; }` β€” the `&` makes changes persist back to the caller's variable (default is pass-by-value, a copy). [S1] - **Variadic functions (`...`)** β€” `function sumMyNumbers(...$x) {...}` β€” accepts an unknown number of arguments, collected into an array; must be the LAST parameter. [S1] - **Loosely typed by default** β€” no type declarations required. [S1] - **`declare(strict_types=1);`** (PHP 7+) β€” must be the FIRST line of the file; enforces strict type checking, throwing a Fatal Error on type mismatch. [S1] - **Return type declarations** β€” `function addNumbers(float $a, float $b) : float {...}` β€” colon syntax before the opening brace. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Default parameter: `function setHeight($height = 50) { echo "The height is : $height
"; } setHeight(350); setHeight(); // uses default 50`. [S1] - Pass by reference: `function add_five(&$value) { $value += 5; } $num = 2; add_five($num); echo $num; // 7`. [S1] - Variadic (valid, last position): `function sumMyNumbers(...$x) { $n = 0; $len = count($x); for($i = 0; $i < $len; $i++) { $n += $x[$i]; } return $n; }`. [S1] - Variadic (invalid, first position β€” errors): `function myFamily(...$firstname, $lastname) {...} // error: variadic must be last`. [S1] - Strict types: `