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

Named Arguments (PHP 8.0)

php PHP 8.0+ Beginner

Also Known As

PHP named arguments named parameters PHP 8 named args

TL;DR

Pass arguments to a function by parameter name rather than position, improving readability and allowing optional parameters to be skipped.

Explanation

Named arguments (PHP 8.0+) allow calling functions with label: $value syntax, making long argument lists self-documenting and enabling skipping of optional parameters without passing null placeholders. For example, array_slice($array, offset: 2, preserve_keys: true) is clearer than array_slice($array, 2, null, true). They also work with built-in functions and constructors. Named arguments are positional-order-independent, but must come after positional arguments.

Common Misconception

Named arguments are just more verbose positional arguments. Named arguments allow skipping optional parameters, improve readability of calls with many arguments, and are order-independent — they also make code more resilient to function signature changes that add parameters in the middle.

Why It Matters

Named arguments (PHP 8.0+) let you pass arguments by parameter name in any order — they make calls with many optional parameters self-documenting and eliminate error-prone positional argument counting.

Common Mistakes

  • Using named arguments for all calls — they are most valuable when skipping optional parameters or for clarity.
  • Renaming function parameters without treating it as a breaking change — named argument callers break silently.
  • Not using named arguments with built-in functions that have confusing parameter order — e.g., array_slice($arr, offset: 2, length: 5).
  • Mixing positional and named arguments where the positional arguments come after named ones — PHP requires positional arguments first.

Code Examples

✗ Vulnerable
// Cryptic positional arguments:
$result = array_slice($users, 1, 5, true); // What does 'true' do here?

// Self-documenting with named arguments:
$result = array_slice($users, offset: 1, length: 5, preserve_keys: true);
✓ Fixed
// Before: cryptic positional arguments
$result = array_slice($array, 2, null, true);

// After: self-documenting with named args
$result = array_slice(array: $array, offset: 2, preserve_keys: true);

// Skip optional parameters you don't need
str_contains(haystack: $html, needle: '</body>');

// Named args in constructors — order doesn't matter
$req = new CreateUserRequest(
    email: 'alice@example.com',
    role:  'admin',
    name:  'Alice',
);

Tags


Added 15 Mar 2026
Edited 22 Mar 2026
Views 32
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 0 pings S 1 ping M 1 ping T 0 pings W 1 ping T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T 2 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S
No pings yet today
Amazonbot 9 Perplexity 9 Ahrefs 2 Unknown AI 2 SEMrush 2 Majestic 1 ChatGPT 1 Google 1
crawler 27
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Use named arguments for functions with many parameters to make call sites self-documenting: array_slice(array: $arr, offset: 2, length: 5)
📦 Applies To
PHP 8.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Function calls with many positional arguments where intent is unclear; null placeholders for optional params
Auto-detectable: ✓ Yes rector phpcs
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✓ Auto-fixable Fix: Low Context: Function

✓ schema.org compliant