🐘 PHP Beginner

What are named arguments in PHP 8?

Answer

Named arguments (PHP 8.0) allow you to pass arguments to a function by specifying the parameter name, regardless of their position in the signature. Example: array_slice(array: $arr, offset: 1, length: 3, preserve_keys: true);. Benefits: you can skip optional parameters you do not need (no need to pass null placeholders), the code is self-documenting (argument purpose is explicit), and argument order does not matter. Named arguments also work with built-in PHP functions. They are particularly useful for functions with many optional parameters — you only name the ones you want to set, and the rest use their defaults. Named arguments cannot be combined with positional arguments in ambiguous ways.