Default Argument Values
Not every caller wants to specify every parameter. Default argument values let you give a parameter a fallback value that is used automatically whenever the caller leaves that argument out. This turns rigid, always-pass-everything functions into flexible ones where only the interesting parameters need to be supplied at each call site.
Basic syntax
You assign a default with = right in the parameter list. If the caller omits that argument, PHP substitutes the default; if the caller supplies a value, the default is ignored entirely.
A parameter with a default value
<?php
function power($base, $exponent = 2) {
return $base ** $exponent;
}
echo power(5);
echo "\n";
echo power(5, 3);25 125
Defaults must come after required parameters
As a rule, once a parameter has a default value, every parameter after it must also have one. PHP needs to know, from left to right, which arguments in a positional call belong to which parameter — if a required parameter came after an optional one, an omitted middle argument would be ambiguous.
A required parameter placed after a default is a problem
<?php
// Deprecated / disallowed ordering:
function schedule($title, $time = "09:00", $attendee) {
return "{$title} at {$time} with {$attendee}";
}PHP Deprecated: Optional parameter $time declared before required parameter $attendee is implicitly treated as a required parameter
Modern PHP does not hard-crash on this ordering, but it deprecates it and effectively treats $time as required anyway, defeating the point of giving it a default. The straightforward fix is to reorder parameters so required ones come first:
Correct ordering
<?php
function schedule($title, $attendee, $time = "09:00") {
return "{$title} at {$time} with {$attendee}";
}
echo schedule("Standup", "Marta");Standup at 09:00 with Marta
Named arguments as an escape hatch
Sometimes you genuinely want a required parameter to appear after several optional ones in the function's signature — perhaps because that ordering reads better. PHP 8's named arguments let callers skip straight to a specific parameter by name, sidestepping strict left-to-right positional filling entirely. That feature has enough depth to deserve its own page (see Named Arguments), but it's worth knowing it exists as the escape hatch for ordering constraints.
Default values must be constant expressions
A default value has to be something PHP can resolve without running any of your business logic — a literal, a const, an enum case, or a simple constant expression built from those. You cannot default a parameter to the result of calling another function or to a variable from the surrounding scope.
Constant expressions are allowed, function calls are not
<?php
const DEFAULT_LIMIT = 25;
function fetchItems($page = 1, $limit = DEFAULT_LIMIT) {
return "page {$page}, limit {$limit}";
}
echo fetchItems();
// This would NOT compile:
// function fetchItems($page = 1, $limit = getConfiguredLimit()) { ... }page 1, limit 25
PHP 8.1: new objects as default values
Before PHP 8.1, you could not write function f($logger = new NullLogger()), because new SomeClass() was not considered a constant expression. PHP 8.1 introduced "new in initializers," specifically legalizing new expressions in default parameter values (and a few other places), which is very convenient for dependency-injection-style defaults.
new as a default value (PHP 8.1+)
<?php
class NullLogger {
public function log($message) {
// intentionally does nothing
}
}
class FileLogger {
public function log($message) {
echo "LOG: {$message}";
}
}
function process($data, NullLogger|FileLogger $logger = new NullLogger()) {
$logger->log("Processing started");
return strtoupper($data);
}
echo process("done");
echo "\n";
echo process("done", new FileLogger());DONE LOG: Processing started DONE
A default's value is fixed at compile time — literals, class constants, enum cases, and (from PHP 8.1) 'new' expressions are allowed.
Plain variables, function calls, and method calls are not allowed as default values.
Array literals built purely from constants are allowed, e.g.
$sizes = [SMALL, MEDIUM].