PHP › Functions & Scope

What are named arguments introduced in PHP 8, and how do they improve the readability of function calls with many parameters?

Intermediate

Answer

Named arguments let you specify function arguments by explicitly stating the parameter name they correspond to, rather than relying purely on their position in the function's parameter list, which is especially useful for functions with many parameters or several optional parameters, since you can skip parameters you want to leave at their default value and clearly communicate exactly what each provided value represents directly at the call site.
function createUser($name, $email, $isAdmin = false, $isActive = true) {
}
createUser(name: 'Ali', email: 'ali@example.com', isAdmin: true);
Real-world example A developer calling a function with several boolean parameters uses named arguments to make the call site immediately clear about what each true or false value actually represents, rather than leaving future readers to count parameter positions and check the function definition.

Common follow-ups: Can you mix positional and named arguments in the same function call?;What happens if you use a named argument that does not match any parameter name in the function?