PHP Named Arguments

Summary: in this tutorial, you will learn about PHP named arguments and how to use them effectively in your code.

Introduction to the PHP named arguments

Since PHP 8.0, you can use named arguments for functions. The named arguments allow you to pass arguments to a function based on the parameter names rather than the parameter positions.

The following example defines a function that finds the position of the first occurrence of a substring in a string:

<?php

function find($needle, $haystack)
{
    return strpos($haystack, $needle);
}    
Code language: HTML, XML (xml)

To call the find() function, you pass the arguments based on the parameter positions like this:

find('awesome', 'PHP is awesome!');Code language: JavaScript (javascript)

In this case, $needle is 'awesome' and $haystack is 'PHP is awesome!'.

However, the function call is not apparent. For example, you don’t know which argument is the needle and which argument is the haystack.

Sometimes, you may accidentally make a mistake by passing the arguments in the wrong order. For example:

find ( 
    'PHP is awesome!',
    'awesome'
);Code language: JavaScript (javascript)

This is buggy and very difficult to troubleshoot.

To avoid this, you may add comments to the arguments like this:

find (
    'awesome',        // needle
    'PHP is awesome!' // haystack
);Code language: JavaScript (javascript)

The comment makes the code more clear. However, it’s not robust.

To improve this, PHP 8.0 introduced the named arguments that allow you to specify the parameter names when passing arguments:

find (
    $needle : 'awesome',
    $haystack : 'PHP is awesome!'
);Code language: PHP (php)

Since you are using the parameter names, the positions are not necessary. For example, you can swap the parameters like this:

find(
    $haystack :'PHP is awesome!',
    $needle : 'awesome'
);Code language: PHP (php)

It should work correctly.

Skipping default arguments

The following defines a function that creates an anchor element (<a>) from text, href, title, and target:

<?php

function create_anchor(
    $text,
    $href = '#',
    $title = '',
    $target = '_self'
)
{
    $href = $href ? sprintf('href="%s"', $href) : '';
    $title = $title ? sprintf('title="%s"', $title) : '';
    $target = $target ? sprintf('target="%s"', $target) : '';

    return "<a $href $title $target>$text</a>";
}Code language: HTML, XML (xml)

To create a link with the target is _blank, you must specify all the default arguments until the one you want to change. For example:

$link = create_anchor(
    'PHP Tutorial', 
    'https://www.phptutorial.net/',
    '',
    '_blank'
);

echo $link;Code language: PHP (php)

Output:

<a href="https://www.phptutorial.net/"  target="_blank">PHP Tutorial</a>
Code language: HTML, XML (xml)

In this example, you need to pass the space (”) to the third argument. If you use the named arguments, you don’t have to specify all the defaults. For example:

 $link = create_anchor(
    text : 'PHP Tutorial', 
    href : 'https://www.phptutorial.net/',
    target: '_blank'
);Code language: PHP (php)

Mixing named arguments with positional arguments

PHP allows you to call a function by using both positional arguments and named arguments. And you need to place the named arguments after positional arguments. For example:

$link = create_anchor(
    'PHP Tutorial', 
    'https://www.phptutorial.net/',
     target: '_blank'
);Code language: PHP (php)

In this example:

  • The text is 'PHP Tutorial'.
  • The href is 'https://www.phptutorial.net/'.
  • And the target is '_blank'.

If you place the named arguments before the positional arguments, you’ll get an error. For example:

create_anchor(
    target : '_blank',
    'PHP Tutorial', 
    'https://www.phptutorial.net/'
);Code language: JavaScript (javascript)

Error:

Cannot use positional argument after named argumentCode language: PHP (php)

Summary

  • Use PHP named arguments to pass arguments to a function based on the parameter names.
  • Put the named arguments after the positional arguments in function calls.
Did you find this tutorial useful?