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

Named Arguments (PHP 8.0)

php PHP 8.0+ Beginner

TL;DR

PHP 8.0 named arguments let you pass values by parameter name — skipping optional params, self-documenting calls, and enabling out-of-order argument passing.

Explanation

named_arg: value syntax at call sites: array_slice($arr, offset: 1, length: 3). Benefits: skip optional params with defaults, document complex calls, reorder args. Works with built-in functions too: htmlspecialchars($str, ENT_QUOTES | ENT_SUBSTITUTE, encoding: 'UTF-8'). In attributes: #[Route(path: '/home', methods: ['GET'])]. Named args cannot be combined with positional args in PHP 8.0 (fixed in 8.1). Spreading named args: fn(...$named). Cannot use named args for constructors with property promotion before 8.1. Named args in array_slice removed need to pass all earlier args.

Common Misconception

Named arguments change how functions are implemented — they only affect call sites. The function signature is unchanged.

Why It Matters

Named arguments eliminate the need to remember parameter order for functions with many optional params — making complex built-in function calls self-documenting.

Common Mistakes

  • Using named args for the first positional argument — only useful for optional params.
  • Mixing positional and named args incorrectly (positional must come first).
  • Not using named args in attributes where they dramatically improve readability.

Code Examples

✗ Vulnerable
array_slice($array, 1, 3, true); // What is true?
setcookie('session', $val, 0, '', '', true, true); // What are the booleans?
✓ Fixed
array_slice($array, offset: 1, length: 3, preserve_keys: true);

setcookie(
    name: 'session',
    value: $val,
    secure: true,
    httponly: true
);

Added 23 Mar 2026
Views 20
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping F 1 ping S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 1 ping S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S
No pings yet today
No pings yesterday
Amazonbot 8 Unknown AI 3 Perplexity 2 ChatGPT 1 Google 1 Ahrefs 1
crawler 15 pre-tracking 1
DEV INTEL Tools & Severity
🔵 Info ⚙ Fix effort: Low
⚡ Quick Fix
Add named arguments to complex built-in calls (array_slice, setcookie, preg_replace). Always use named args in attribute constructors with multiple params.
📦 Applies To
PHP 8.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
setcookie\(|array_slice\(
Auto-detectable: ✗ No rector
🤖 AI Agent
Confidence: Low False Positives: High ✓ Auto-fixable Fix: Low Context: Line

✓ schema.org compliant