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

Named Arguments in Built-in PHP Functions

php PHP 8.0+ Intermediate

Also Known As

named args in built-ins PHP built-in named parameters

TL;DR

PHP 8.0 named arguments work with built-in functions — enabling readable calls to functions with many optional parameters like array_slice().

Explanation

Named arguments (PHP 8.0) apply to built-in functions as well as userland code. This is particularly valuable for functions with many positional parameters where intermediate defaults must be skipped: array_slice($array, offset: 2, preserve_keys: true) is far clearer than array_slice($array, 2, null, true). Common built-in targets: str_contains, implode, array_map, array_filter (with mode:), htmlspecialchars (flags:, encoding:), json_encode (flags:, depth:). Caution: built-in parameter names are part of PHP's public API from 8.0 onward but some names changed between versions — check the manual. Named arguments cannot be used with variadic functions that spread into ...$args.

Common Misconception

Named arguments work identically for all PHP built-in functions. Built-in function parameter names are part of the PHP specification from 8.0+ but may differ between extensions and versions — named arguments on internal functions should be used with caution when writing code that must run on multiple PHP versions.

Why It Matters

PHP 8's named arguments work with all built-in functions — they are especially valuable for functions with many optional parameters like array_slice, implode, or htmlspecialchars where positional order is easy to get wrong.

Common Mistakes

  • Not using named arguments for built-in functions with confusing parameter order — str_contains vs strpos argument order differs.
  • Renaming function parameters in your own API and forgetting it is a breaking change for named argument callers.
  • Using named arguments with variadic functions — they interact in non-obvious ways.
  • Not using named arguments for clarity in calls with boolean flags: htmlspecialchars($str, double_encode: false).

Code Examples

✗ Vulnerable
// Confusing positional arguments:
$result = array_slice($arr, 1, 5, true); // What is 'true'?
$encoded = htmlspecialchars($str, ENT_QUOTES | ENT_HTML5, 'UTF-8', false); // Last arg?

// Named — self-documenting:
$result = array_slice($arr, offset: 1, length: 5, preserve_keys: true);
$encoded = htmlspecialchars($str, double_encode: false);
✓ Fixed
// Skip intermediate positional arguments cleanly
$slice = array_slice($items, offset: 5, preserve_keys: true);
$encoded = json_encode($data, flags: JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);

Added 15 Mar 2026
Edited 22 Mar 2026
Views 23
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 1 ping T 1 ping F 0 pings S 0 pings S 1 ping 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 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 2 pings F 0 pings S
No pings yet today
Amazonbot 1
Amazonbot 10 Perplexity 3 Unknown AI 2 Ahrefs 2 Google 2
crawler 18 crawler_json 1
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Low
⚡ Quick Fix
When designing PHP 8+ APIs avoid renaming function parameters in minor versions — parameter names are part of your public API contract when named arguments are used
📦 Applies To
PHP 8.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Library function parameter renamed in minor version breaking callers using named arguments
Auto-detectable: ✓ Yes phpstan psalm rector
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✓ Auto-fixable Fix: Low Context: Line

✓ schema.org compliant