PHP – 8.0 New Features Named Arguments

PHP, and many other programming languages, support positional parameters: The caller passes the parameters in the same order the function/method declares its parameters.

In PHP 8.0, Named Parameters support is added, which means it’s now possible to call a function/method by setting the parameters by their name.

Named Arguments (introduced in PHP 8) let you pass values to a function by specifying parameter names, instead of relying only on the order of arguments.

This makes your code:

More readable

More flexible (you can skip optional parameters)

Less error-prone when functions have many parameters

Example: Without Named Arguments

<?php
function createUser(string $name, int $age, bool $isAdmin = false) {
    return "$name is $age years old. Admin: " . ($isAdmin ? 'Yes' : 'No');
}

// Call (must follow parameter order)
echo createUser("Alice", 30, true);

?>
Here, you need to pass all arguments in order, even if you only want to change the last one.

Example: With Named Arguments (PHP 8+)

<?php
function createUser(string $name, int $age, bool $isAdmin = false) {
    return "$name is $age years old. Admin: " . ($isAdmin ? 'Yes' : 'No');
}

// Call using names
echo createUser(age: 30, name: "Alice", isAdmin: true);

?>

Parameters can be passed in any order.
Optional parameters can be skipped:

<?php echo createUser(name: "Bob", age: 25); // isAdmin defaults to false ?>

Combining Positional + Named Arguments

<?php
function sendMail(string $to, string $subject, string $message, bool $isHtml = false) {}

sendMail("test@example.com", "Hello", message: "Welcome!", isHtml: true);

?>

Rule: Positional arguments must come first, then named arguments.

Restrictions

No duplicate names

// ❌ Error
createUser(name: "Alice", name: "Bob", age: 30);

Cannot mix positional after named

<?php
// ❌ Error
createUser(name: "Alice", 30);

?>

Relies on parameter names → changing parameter names in function definition can break external calls using named arguments.

Benefits of Named Arguments

Clearer and more self-documenting function calls

Reduces mistakes with many optional parameters

Great for functions with default values and configuration-like options