← Back to PHP Course | Chapter 5: Functions | Lesson 4 of 10

PHP Default Arguments

A default argument is a backup choice used when you do not say otherwise, like a pizza that comes with cheese unless you ask for something else. It lets callers leave a blank out.
Syntax
php
function function_name($required, $optional = default_value) {
    // body
}

function_name($a);       // uses the default
function_name($a, $b);   // overrides it

What are Default Arguments?

Assigning a value directly in a parameter's definition (function greet($name = Guest)) makes that argument optional at the call site — if the caller doesn't supply one, PHP quietly falls back to the default instead of raising an error about a missing argument.

Example: What are Default Arguments?

php
<?php
// Define the function `greet` taking `$name`
function greet($name = 'Guest') {
    // Print "Hello, $name!" to the output
    echo "Hello, $name!";
}
// Call `greet()`
greet();
?>

Placing Default Arguments

PHP requires every parameter that has a default value to come *after* every parameter that doesn't. Putting an optional parameter before a required one creates an ordering PHP can't resolve unambiguously, and it will refuse to parse the function definition at all.

Example: Placing Default Arguments

php
<?php
// Define the function `greet` taking `$name`, `$greeting`
function greet($name, $greeting = "Hello") {
    // Print "$greeting, $name!" to the output
    echo "$greeting, $name!";
}
// Call `greet("Alice")`
greet("Alice");
?>

Overriding Default Values

Calling the function normally and simply supplying a value overrides that parameter's default for that one call — defaults only ever apply when an argument is genuinely omitted, never when one is explicitly passed, even if it happens to match the default.

Example: Overriding Default Values

php
<?php
// Define the function `greet` taking `$name`
function greet($name = 'Guest') {
    // Print "Hello, $name!" to the output
    echo "Hello, $name!";
}
// Call `greet("Alice")`
greet("Alice");
?>

Type Declarations with Defaults

Type declarations and default values work together without conflict: PHP still validates the type of any value the caller actually supplies, while the default itself is exempt from that check (it's trusted as already correct when you wrote it).

Example: Type Declarations with Defaults

php
<?php
// Define the function `setVolume` taking `int $level`
function setVolume(int $level = 50) {
    // Print "Volume: $level" to the output
    echo "Volume: $level";
}
// Call `setVolume()`
setVolume();
?>

Null vs Default Argument

Explicitly passing null is treated as a real argument, not as 'nothing was passed' — it does *not* trigger the parameter's default value.

If a parameter should genuinely fall back to its default on null too, that has to be handled inside the function body, not by the default-argument mechanism alone.

Example: Null vs Default Argument

php
<?php
// Define the function `greet` taking `$name`
function greet($name = 'Guest') {
    // Declare `$name`, set to `$name ?? 'Guest'`
    $name = $name ?? 'Guest';
    // Print "Hello, $name!" to the output
    echo "Hello, $name!";
}
// Call `greet(null)`
greet(null);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Placing a default parameter before a required one, like function f($a = 1, $b), which is deprecated in PHP 8.
  2. Expecting null passed explicitly to trigger the default, when defaults apply only if the argument is omitted.
  3. Using a non-constant expression as a default in older PHP versions, when new in initializers is allowed only from PHP 8.1.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.