Named Arguments
Named arguments (PHP 8.0) let you pass values by parameter name instead of position. They make complex function calls readable, let you skip optional parameters, and serve as inline documentation.
// setcookie has 7 parameters. Which is which?
setcookie('theme', 'dark', 0, '/', '', true, true);
// Named arguments: crystal clear intent
setcookie('theme', 'dark', httponly: true, secure: true);
// Perfect for functions with boolean flags
$text = str_pad($input, length: 20, pad_type: STR_PAD_LEFT);
// Named arguments + array unpacking = powerful patterns
$defaults = ['secure' => true, 'httponly' => true, 'samesite' => 'Strict'];
setcookie('token', $value, ...$defaults);
// Great for test readability
$user = UserFactory::create(
name: 'Alice',
email: 'alice@example.com',
role: Role::Admin,
verified: true,
);
Named arguments work with all functions — built-in and user-defined. They interoperate with positional arguments (positional first, then named). They even work with phpcall_user_func_array when you pass an associative array.
Significance: Readability
Named arguments turn cryptic function calls into self-documenting expressions. They're especially powerful for PHP's large standard library, where many functions have accumulated optional parameters over decades. You no longer need to memorize parameter positions.