Default and Variadic Parameters
Use default values and variadic arguments in PHP functions.
Default Parameter Values
Default values let callers omit arguments. The default is used when the argument is not passed:
<?php
function createUser(string $name, string $role = 'viewer'): string {
return "$name ($role)";
}
echo createUser('Alice'); // Alice (viewer)
echo createUser('Bob', 'admin'); // Bob (admin)Default Value Rules
Important rules for default parameters:
- Parameters with defaults must come after required parameters
- Defaults must be constant expressions — not function calls or variables
- PHP 8 named arguments relax the ordering requirement
All lessons in this course
- Defining and Calling Functions
- Default and Variadic Parameters
- Variable Scope: Local and Global
- Anonymous Functions and Closures