PHP Reference ยท Functions and Classes
Default parameter values in PHP
Reference for PHP default parameter values: optional arguments, allowed default expressions, null defaults, ordering rules, named arguments and common mistakes.
A parameter with a default value is optional. If the caller leaves it out, PHP uses the default; if the caller passes a value, that value wins. Defaults let one function serve both the simple case and the detailed case without overloading.
You write the default after an equals sign in the parameter list. Since PHP 8.1 the default can also be a new expression in some places, and since 8.0 you can skip optional arguments by naming the ones you do pass.
function tag(string $text, string $element = 'p'): string { return "<$element>$text</$element>"; } echo tag('Hi'); // <p>Hi</p> echo tag('Hi', 'h1'); // <h1>Hi</h1>
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
function name(Type $required, Type $optional = expression, ?Type $maybe = null)
| Part | Description |
|---|---|
= expression |
A constant expression: literals, constants, class constants, [], enum cases, arithmetic and concatenation of those, and (PHP 8.1+) new ClassName(...) for parameters, static variables and global constants. |
?Type $x = null |
Optional and nullable. Passing null explicitly is also allowed. |
Type $x = null |
Implicit nullable type. Deprecated in PHP 8.4: write ?Type instead. |
Errors: omitting a required argument throws ArgumentCountError. A non-constant default (such as a function call) is a compile error.
Usage notes
- Put optional parameters last. In
function odd($a = 1, $b)the default of$ais useless, so PHP 8.0+ emits a deprecation notice and treats$aas required. - Default means "argument omitted", not "null passed".
connect('h', null)for anint $portthrowsTypeError; only a nullable type acceptsnull. - Only constant expressions. You cannot write
$when = time()or$size = count($x). Usenullas a sentinel and compute inside:$when ??= time();. - No shared state. Unlike some languages, a mutable-looking default such as
[]is a fresh value on every call. - Skipping with named arguments.
connect('h', secure: true)leaves$portat its default. Positional calls cannot skip a parameter. - Adding a defaulted parameter at the end of an existing function is backward compatible. Reordering is not.
- Reflection.
ReflectionParameter::getDefaultValue()reads the default;isDefaultValueAvailable()tells whether one exists. - Variadic parameters (
...$rest) cannot have defaults; they are an empty array when no extras are passed. - Defaults in interfaces and overrides. A child method may change the default value; calls use the default of the static type known at the call site.