0PricingLogin
PHP Academy · Lesson

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

  1. Defining and Calling Functions
  2. Default and Variadic Parameters
  3. Variable Scope: Local and Global
  4. Anonymous Functions and Closures
← Back to PHP Academy