PHP 8 Intermediate Language Basics · 7 min read

PHP Named Arguments

Lesson 31 of 43 in the PHP 8 learning path.

How do I call a function with named arguments so defaults stay clear?

Named arguments pass a value by the parameter name. You can skip showNav and set showFooter, and the skipped parameter keeps its default. You can also put the names in any order. PHP fills the parameters by name, not by the position of those named arguments. A name that is not a real parameter throws Error.

Names are part of the function contract. Renaming a parameter breaks callers that use that name, so public parameter names should stay stable. Do not put a positional argument after a named one. That order is a syntax error. Use names when a call has several booleans and a reader would otherwise have to count positions.

Syntax & Parameters

Syntax
renderPage(title: $title, body: $body, showFooter: false)

Example

src/Page.php
<?php

declare(strict_types=1);

function escapeHtml(string $value): string
{
    return htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}

function renderPage(
    string $title,
    string $body,
    bool $showNav = true,
    bool $showFooter = true,
    string $lang = 'en',
): string {
    $html = '<article lang="' . escapeHtml($lang) . '">';
    if ($showNav) {
        $html .= '<nav>Catalog</nav>';
    }
    $html .= '<h1>' . escapeHtml($title) . '</h1>';
    $html .= '<p>' . escapeHtml($body) . '</p>';
    if ($showFooter) {
        $html .= '<footer>Uptoolify</footer>';
    }
    $html .= '</article>';

    return $html;
}

echo renderPage(
    title: 'Named arguments',
    body: 'Defaults stay in place.',
    showFooter: false,
) . "\n";

echo renderPage(
    body: 'Language is explicit.',
    title: 'Second page',
    lang: 'en-PH',
) . "\n";

try {
    renderPage(title: 'Broken', body: 'Call', theme: 'dark');
} catch (Error $error) {
    echo 'unknown: ' . $error->getMessage() . "\n";
}

Expected Output

<article lang="en"><nav>Catalog</nav><h1>Named arguments</h1><p>Defaults stay in place.</p></article>
<article lang="en-PH"><nav>Catalog</nav><h1>Second page</h1><p>Language is explicit.</p><footer>Uptoolify</footer></article>
unknown: Unknown named parameter $theme

Line-by-Line Walkthrough

  1. escapeHtml uses ENT_QUOTES, ENT_SUBSTITUTE, and UTF-8 so a title cannot break out of the heading.
  2. renderPage defaults showNav and showFooter to true and lang to en.
  3. The nav and the footer are included only when their flags are true. The title and the body are escaped.
  4. The first call names title, body, and showFooter. showNav is omitted, so the default keeps the nav.
  5. showFooter false removes the footer on that first page.
  6. The second call passes body before title. The names decide which parameter receives which string.
  7. lang is en-PH on the second call. Both flags are omitted, so the nav and the footer stay on.
  8. The third call passes theme, which is not a parameter of renderPage.
  9. PHP throws Error. The catch prints Unknown named parameter $theme.
  10. The two pages are printed before that probe, so the bad name does not erase the renders that succeeded.

Common Pitfalls & Security

The parameter name is public once callers use it. A rename is a breaking change, just like renaming a function.

  • A positional argument after a named argument is a syntax error. Names can follow positionals, not the other way around.
  • An unknown name throws Error at the call. Catching that around a normal render hides a bug. The sample catches it only to show the message.
  • Skipping a parameter that has no default is still an argument error. Names do not make a required parameter optional.
  • Passing the same parameter twice, once by position and once by name, throws. Say it once.
  • Boolean flags stay easy to mix up if some calls use names and others rely on position. Prefer names for a run of booleans.
  • Named arguments do not escape HTML. This function escapes because it writes tags, not because the arguments are named.

Best Practices & Modern Standards

Use named arguments when a reader would otherwise count past several defaults, especially booleans. Leave the defaults you want by omitting those names. Put required values first in the signature so positional callers stay natural.

Treat parameter names as stable API. Do not rename them to make a sentence prettier after other code calls them by name. Keep side effects out of default expressions, because a skipped argument does not evaluate a replacement you pass elsewhere.

Real-World Use

The layout call turns the footer off by name and leaves the nav on its default, so nobody passes a dummy true just to reach the footer flag. A second call sets lang to en-PH and passes the body before the title without swapping their meaning.

Frequently Asked Questions

Can I skip a parameter that has a default?

Yes. Omit the name and the default is used. In the first call showNav stays true while showFooter is set to false.

Can I put a positional argument after a named one?

No. That order is a syntax error. You can start with positional arguments and then switch to names for the rest of the call.

What if the name is not a parameter?

PHP throws Error with the message Unknown named parameter and the name. Fix the call instead of catching that error in production rendering.

Do named arguments change escaping?

No. They only choose parameters. Escape text when you place it in HTML, the same way you would for a positional call.

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share

Reactions

Loading reactions…