Default and Variadic Parameters
Use default values and variadic arguments in PHP functions.
Default and Variadic Parameters is a free PHP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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
Nullable Parameters
Use ?type to allow NULL as a valid argument, with NULL as default:
<?php
function findUser(?int $id = null): string {
if ($id === null) {
return 'Searching all users';
}
return "User #$id";
}
echo findUser(); // Searching all users
echo findUser(42); // User #42Variadic Functions with ...
Use ... before a parameter to accept a variable number of arguments as an array:
<?php
function sum(int ...$numbers): int {
return array_sum($numbers);
}
echo sum(1, 2, 3); // 6
echo sum(10, 20, 30, 40); // 100Variadic with Required Params
You can mix required parameters with a variadic at the end:
<?php
function logMessage(string $level, string ...$messages): void {
foreach ($messages as $msg) {
echo "[$level] $msg" . PHP_EOL;
}
}
logMessage('INFO', 'App started', 'Config loaded');Spread Operator in Call
Use ... when calling a function to spread an array as individual arguments:
<?php
function add3(int $a, int $b, int $c): int {
return $a + $b + $c;
}
$args = [1, 2, 3];
echo add3(...$args); // 6
$extra = [4, 5];
echo add3(1, ...$extra); // 10Named Arguments (PHP 8)
Named arguments let you pass values by parameter name, ignoring order:
<?php
function createTag(string $tag, string $content, string $class = ''): string {
$cls = $class ? " class=\"$class\"" : '';
return "<$tag$cls>$content</$tag>";
}
// Skip $class, pass $content by name
echo createTag(content: 'Hello', tag: 'p');
// <p>Hello</p>Named Arguments with Built-ins
Named arguments work with PHP built-in functions too:
<?php
$arr = [3, 1, 4, 1, 5];
// Traditional positional
$result1 = array_slice($arr, 1, 3);
// Named arguments
$result2 = array_slice(array: $arr, offset: 1, length: 3);
print_r($result1); // [1, 4, 1]func_get_args() Legacy
Before PHP 5.6 variadic syntax, func_get_args() was used to access all arguments. You may see it in legacy code:
<?php
function legacySum(): int {
$args = func_get_args();
return array_sum($args);
}
echo legacySum(1, 2, 3, 4); // 10
// Modern: use int ...$numbers insteadType-Hinted Variadic
Combine type hints with variadic parameters for strict input:
<?php
declare(strict_types=1);
function joinStrings(string $sep, string ...$parts): string {
return implode($sep, $parts);
}
echo joinStrings('-', 'a', 'b', 'c'); // a-b-c
// joinStrings('-', 'a', 42); // TypeError in strict modeCombining Default and Named
Named arguments and defaults work together to give maximum flexibility:
<?php
function sendEmail(
string $to,
string $subject,
string $body = '',
bool $html = false
): bool {
// simulate send
return true;
}
// Skip $body, pass $html by name
sendEmail('a@b.com', 'Hi', html: true);Quick Check
What does the ... operator do in a PHP function definition?
Recap: Default and Variadic Params
Summary:
- Default values allow optional parameters
- Parameters with defaults go after required ones
?typeallows NULL with optional default...$argscollects variadic arguments into an array- Spread operator
...$arrexpands an array at call site - Named arguments (PHP 8) pass by name, not position
Frequently asked questions
Is the “Default and Variadic Parameters” lesson free?
Yes — the full text of “Default and Variadic Parameters” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Default and Variadic Parameters”?
Use default values and variadic arguments in PHP functions. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Default and Variadic Parameters” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Defining and Calling Functions
- Default and Variadic Parameters
- Variable Scope: Local and Global
- Anonymous Functions and Closures