📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials PHP for Beginners Named Arguments

Named Arguments

4 min read Quiz at the end
Named arguments pass values to a function by parameter name instead of position. This makes function calls easier to read and lets you skip optional parameters you do not need to change.

Named Arguments (PHP 8)

function createUser(
    string $name,
    int $age = 0,
    string $role = "user",
    bool $active = true
): void { /* ... */ }

// Old way — must pass in order
createUser("Alice", 0, "admin", true);

// Named arguments — any order, skip defaults
createUser(name: "Alice", role: "admin");

// Useful with built-in functions
array_slice(array: $items, offset: 2, length: 5);
str_contains(haystack: $text, needle: "PHP");
implode(separator: ", ", array: $list);
Topic Quiz · 5 questions

Test your understanding before moving on

1. Named arguments are passed using:
💡 Named arguments use the syntax: functionName(paramName: value).
2. Do named arguments have to follow parameter order?
💡 Named arguments allow you to pass arguments in any order by using parameter names.
3. Can you skip optional parameters with named args?
💡 Named arguments let you skip optional parameters without passing placeholder values.
4. Named arguments were introduced in PHP:
💡 Named arguments were introduced in PHP 8.0.
5. Which is a valid named argument call?
💡 Named arg names match the parameter names in the function definition.