ZetCode

PHP default Keyword

last modified November 25, 2026

The PHP default keyword marks the fallback branch of a switch statement or a match expression. It runs when no other case or arm matches the tested value.

This article covers the fallback label: how switch chooses it, why its position matters, how it falls through like any other case, the switch (true) range pattern, and the stricter behaviour of match when a default is missing.

Basic definitions

In a switch, default: is a label followed by statements, just like case. PHP compares the switched value loosely with each case in source order; if none is equal, execution starts at the default label. Without a default, an unmatched value simply does nothing.

In a match expression, default => is an arm whose condition always matches. The expression must produce a value on every path, so a match without a default throws UnhandledMatchError when the subject is not listed.

default is not used for function parameter values or array destructuring. Those use the assignment operator, as in function greet(string $name = "guest"); the PHP functions article covers parameters. This page is about the keyword itself.

The PHP switch/case keywords article covers labels and break, and PHP match covers the expression form in depth.

A switch default

The first example maps a role to an access level and uses default for every role that is not listed.

default_switch.php
<?php

declare(strict_types=1);

$role = "editor";

switch ($role) {
    case "admin":
        echo "Full access\n";
        break;
    case "editor":
        echo "Edit access\n";
        break;
    default:
        echo "Read access\n";
}

The value "editor" matches the second case, so the default is skipped entirely. Changing the variable to "auditor" would run the default branch and print the read-only message. Every branch ends with break, which keeps the cases separate; the break statement article explains why.

$ php default_switch.php
Edit access

default may stand anywhere

The default label does not have to be last. PHP compares only the named cases and jumps to the default wherever it appears, so moving it to the top changes nothing about matching.

default_position.php
<?php

declare(strict_types=1);

$role = "guest";

switch ($role) {
    default:
        echo "Unknown role\n";
        break;
    case "admin":
        echo "Full access\n";
        break;
    case "guest":
        echo "Read access\n";
        break;
}

"guest" matches its case even though the default comes first. The order of the labels matters only when several cases share a body or when fall-through is involved. Most code keeps the default last because that mirrors "everything else", but a default first is valid and occasionally reads better as an early rejection.

$ php default_position.php
Read access

default falls through like a case

Once execution enters the default, it continues into the statements of the following labels unless a break stops it. The location of the default therefore matters when it has no break of its own.

default_fallthrough.php
<?php

declare(strict_types=1);

$role = "auditor";

switch ($role) {
    default:
        echo "Unknown role\n";
    case "guest":
        echo "Read access\n";
        break;
    case "admin":
        echo "Full access\n";
        break;
}

"auditor" matches no case, so execution starts at the default, prints the unknown-role line, and then falls into the "guest" body. The reader sees both messages even though only one condition holds. The fix is a break at the end of the default, or placing the default last. Fall-through is useful when cases deliberately share work, but it should never be accidental.

$ php default_fallthrough.php
Unknown role
Read access

Ranges with switch (true)

A switch compares one value against each case, but the compared value can be the boolean true. The cases then become conditions, and the default handles everything that falls outside the listed ranges.

default_ranges.php
<?php

declare(strict_types=1);

$score = 75;

switch (true) {
    case $score >= 90:
        echo "Grade: A\n";
        break;
    case $score >= 80:
        echo "Grade: B\n";
        break;
    case $score >= 70:
        echo "Grade: C\n";
        break;
    default:
        echo "Grade: F\n";
}

The first case whose condition is true wins, so the order from the highest threshold down is essential. A score of 75 fails the first two conditions, matches $score >= 70, and prints the C grade; the default catches anything below 70. This pattern is an alternative to a long if/elseif chain, and the if/else/elseif article compares the two.

$ php default_ranges.php
Grade: C

Default in a match expression

A match arm with default is the equivalent of the switch fallback, except that the arm returns a value and the comparison is strict.

default_match.php
<?php

declare(strict_types=1);

$status = 404;

$message = match ($status) {
    200 => "OK",
    301 => "Moved Permanently",
    404 => "Not Found",
    default => "Unknown status",
};

echo $message, "\n";

404 matches its arm and the default is not evaluated. The expression is an assignment, so the result is available as $message, and no break is needed. match uses ===, so the string "404" would not match the integer arm and would fall to the default. The match keyword article covers the differences from switch.

$ php default_match.php
Not Found

Without a default, match throws

A match without a default must cover every possible value. When the subject is not listed, PHP raises an UnhandledMatchError at run time.

default_match_error.php
<?php

declare(strict_types=1);

$status = 418;

$message = match ($status) {
    200 => "OK",
    404 => "Not Found",
};

echo $message, "\n";

The message is Unhandled match case of type int, and the script stops because nothing catches the error. This is sometimes exactly the desired behaviour: a missing value is a bug, not a case to hide. Switch would have continued silently instead, which is why match is safer for closed sets such as the branches of an enum.

$ php default_match_error.php
Fatal error: Uncaught UnhandledMatchError: Unhandled match case of type int in /app/default_match_error.php:7
Stack trace:
#0 {main}
  thrown in /app/default_match_error.php on line 7

Null and string subjects

A default arm can sit beside typed arms and receive any value that the other arms reject. This example handles a missing value and an unknown format in one expression.

default_match_string.php
<?php

declare(strict_types=1);

function describe(?string $format): string
{
    return match ($format) {
        "json" => "JSON document",
        "csv" => "CSV file",
        null => "No format chosen",
        default => "Unsupported format: " . $format,
    };
}

echo describe("csv"), "\n";
echo describe(null), "\n";
echo describe("yaml"), "\n";

"csv" matches its arm, null matches the explicit null arm, and "yaml" reaches the default. The ?string parameter allows null, and the arm list covers it deliberately rather than relying on a loose comparison. When the set of inputs is open-ended, as with a format name typed by a user, a default is the right choice; when the set is closed, leaving it out exposes mistakes.

$ php default_match_string.php
CSV file
No format chosen
Unsupported format: yaml

Best practices

Use the default as a deliberate decision, not as a way to silence the compiler.

The PHP enum article shows exhaustive matches over cases, and PHP switch/case keywords covers the statement form.

Source

PHP manual: switch

PHP manual: match

This article covered the PHP default keyword: the switch fallback, its free position, fall-through from the default, range checks with switch (true), match arms, and the UnhandledMatchError raised when a match has no default. Continue with PHP match for the full expression, or read PHP switch/case keywords for case labels.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all PHP tutorials.