0Pricing
PHP Academy · Lesson

Match Expression and Named Arguments

Replace verbose switch with match and use named arguments for clarity.

Match Expression and Named Arguments is a free PHP Academy lesson on CoddyKit — lesson 1 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.

The match Expression

PHP 8.0 introduced match, a concise alternative to switch that uses strict equality (===) and returns a value.

match vs switch

Key differences: match uses strict comparison, has no fall-through, and is an expression (returns a value).

<?php
$status = 2;
$label = match($status) {
    1 => "active",
    2 => "inactive",
    3 => "banned",
    default => "unknown",
};
echo $label; // "inactive"

Multiple Conditions per Arm

Separate multiple match values with commas to handle them with the same result.

<?php
$code = 404;
$type = match($code) {
    200, 201, 202 => "success",
    400, 422      => "client error",
    404           => "not found",
    500, 503      => "server error",
    default       => "other",
};
echo $type; // "not found"

match throws on No Match

If no arm matches and there is no default, PHP throws a UnhandledMatchError — unlike switch which silently continues.

match as an Expression

Because match returns a value, you can use it inline — as a function argument, in a return statement, or inside a string interpolation.

<?php
echo "Status: ".match($active) {
    true  => "ON",
    false => "OFF",
};

Named Arguments

PHP 8.0 named arguments let you pass arguments to a function by parameter name, in any order.

<?php
function makeTag(string $tag, string $content, string $class = ""): string {
    return "<$tag class=\"$class\">$content</$tag>";
}
echo makeTag(content: "Hello", tag: "p", class: "lead");

Named Arguments Benefits

Named arguments: improve readability for functions with many parameters, allow skipping optional parameters, and clarify what each value represents.

Named Arguments with Built-ins

Named arguments work with PHP built-in functions too.

<?php
$arr = [3, 1, 4, 1, 5];
$result = array_slice(array: $arr, offset: 1, length: 3, preserve_keys: true);

Combining Named and Positional

You can mix positional and named arguments, but named arguments must come after positional ones.

<?php
str_contains(haystack: "Hello World", needle: "World");
// or positional then named:
str_contains("Hello World", needle: "World");

Named Arguments in array_map

Named arguments are particularly useful with variadic built-ins that have confusing parameter order.

match in Real Code

Replace long if/elseif chains with match for HTTP method routing, status code mapping, and enum-like decisions.

<?php
$handler = match($_SERVER["REQUEST_METHOD"]) {
    "GET"    => new GetHandler(),
    "POST"   => new PostHandler(),
    "DELETE" => new DeleteHandler(),
    default  => throw new InvalidArgumentException("Unsupported method"),
};

Summary

match provides strict, exhaustive, fall-through-free branching. Named arguments improve call-site readability. Both are major PHP 8.0 improvements.

Quick Check

What happens when no match arm matches and there is no default?

Frequently asked questions

Is the “Match Expression and Named Arguments” lesson free?

Yes — the full text of “Match Expression and Named Arguments” 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 “Match Expression and Named Arguments”?

Replace verbose switch with match and use named arguments for clarity. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Match Expression and Named Arguments” 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

  1. Match Expression and Named Arguments
  2. Nullsafe Operator and Union Types
  3. Enumerations (Enums)
  4. Fibers: Cooperative Concurrency
← Back to PHP Academy