Glossary

Named Arguments (PHP 8)

Named arguments — PHP 8.0 allows passing arguments by parameter name rather than position. No need to memorise argument order; optional parameters can be skipped.

Example

// Positional — must remember the order
array_slice($array, 0, 5, true);

// Named — self-documenting without checking the signature
array_slice(
  array: $array,
  offset: 0,
  length: 5,
  preserve_keys: true,
);

// Skip parameters that have defaults
function createUser(
  string $name,
  string $role = 'user',
  bool $active = true
) {}

createUser(name: 'Alice', active: false); // role defaults to 'user'

Limitations

A named argument cannot be passed before a positional one. Named arguments in variadic functions become an associative array.