PHPNamed Arguments (PHP 8)

Named Arguments (PHP 8)

PHP 8.0 introduced named arguments, a syntax that lets you pass arguments to a function by stating the parameter name they belong to, instead of relying purely on position. This might sound like a small convenience, but for functions with several optional parameters — the kind that used to force you to memorize argument order or pass a confusing string of true/false values — named arguments are a genuine readability upgrade.

Basic syntax

Instead of func(value1, value2), you write func(paramName: value1, otherParam: value2). The parameter names come from the function's own declaration, so they have to match exactly.

Calling with named arguments

PHP
<?php
function createUser($name, $role = "member", $active = true) {
    return "{$name} ({$role}) - active: " . ($active ? "yes" : "no");
}

echo createUser(name: "Fatima", role: "admin");
Fatima (admin) - active: yes
Skipping optional parameters out of order

The real payoff shows up when you want to set a parameter that comes after others you're happy to leave at their default. With purely positional arguments, you would have to explicitly repeat every default value in between just to reach the one you care about. Named arguments let you jump straight to it.

Setting only the third parameter

PHP
<?php
function createUser($name, $role = "member", $active = true) {
    return "{$name} ({$role}) - active: " . ($active ? "yes" : "no");
}

// Skip $role entirely, go straight to $active
echo createUser(name: "Wei", active: false);
Wei (member) - active: no
Before and after: unclear boolean flags

The clearest case for named arguments is a function signature packed with booleans or similarly-typed optional flags. Reading a call like renderChart(true, false, true) tells you nothing about what each true or false actually controls without flipping back to the function definition. Naming the arguments turns the call site itself into documentation.

Before: positional booleans are unreadable

PHP
<?php
function renderChart($data, $showLegend = true, $showGrid = false, $animate = true) {
    // ...rendering logic...
    return "legend={$showLegend}, grid={$showGrid}, animate={$animate}";
}

// What do these three booleans even mean at a glance?
echo renderChart($data, false, true, false);

After: named arguments make intent obvious

PHP
<?php
echo renderChart(
    $data,
    showLegend: false,
    showGrid: true,
    animate: false
);
Mixing positional and named arguments

You can combine both styles in a single call, but positional arguments must come first, and once you switch to a named argument, every argument after it must also be named. This mirrors the same left-to-right resolution rule that governs default parameter ordering.

Positional first, then named

PHP
<?php
function bookRoom($building, $room, $capacity = 4, $hasProjector = false) {
    $projector = $hasProjector ? "with projector" : "no projector";
    return "{$building}/{$room}, seats {$capacity}, {$projector}";
}

echo bookRoom("HQ", "Falcon", hasProjector: true);
HQ/Falcon, seats 4, with projector
Positional arguments cannot follow named ones
Writing `bookRoom(building: "HQ", "Falcon")` is a fatal error — `Cannot use positional argument after named argument`. Once you name one argument in a call, every argument that follows it must also be named; you cannot go back to plain positional style partway through the same call.
  • Named arguments must match the exact parameter name from the function signature — renaming a parameter is a breaking change for any caller using named arguments.

  • Argument order no longer matters once every argument is named, so f(b: 2, a: 1) and f(a: 1, b: 2) behave identically.

  • A parameter's default is still used if a named argument for it is simply omitted from the call, exactly like the positional case.

Tip
Reach for named arguments whenever a function call would otherwise need three or more optional parameters, or whenever any of those parameters are booleans. The extra few keystrokes at the call site pay for themselves the first time someone (including future you) reads that line without the function's source open next to it.