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);
}To call the find() function, you pass the arguments based on the parameter positions like this:
find('awesome', 'PHP is awesome!');In this case, $needle is 'awesome' and $haystack is 'PHP is awesome!'.
However, the function call is not so clear. 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'
);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
);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!'
);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'
);It should work correctly.
Here’s the complete code:
<?php
function find($needle, $haystack)
{
return strpos($haystack, $needle);
}
echo find (
needle : 'awesome',
haystack : 'PHP is awesome!'
);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>";
}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://phptutorial.net/',
'',
'_blank'
);
echo $link;Output:
<a href="https://phptutorial.net/" target="_blank">PHP Tutorial</a>In this example, you must pass space (”) to the third argument. Using the named arguments, you don’t have to specify all the defaults. For example:
$link = create_anchor(
text : 'PHP Tutorial',
href : 'https://phptutorial.net/',
target: '_blank'
);Heres’ the complete code:
<?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>";
}
$link = create_anchor(
text : 'PHP Tutorial',
href : 'https://phptutorial.net/',
target: '_blank'
);
echo $link;Mixing named arguments with positional arguments #
PHP allows you to call a function using both positional and named arguments. And you need to place the named arguments after positional arguments. For example:
$link = create_anchor(
'PHP Tutorial',
'https://phptutorial.net/',
target: '_blank'
);In this example:
- The
textis'PHP Tutorial'. - The
hrefis'https://phptutorial.net/'. - And the
targetis'_blank'.
You’ll get an error if you place the named arguments before the positional arguments. For example:
create_anchor(
target : '_blank',
'PHP Tutorial',
'https://phptutorial.net/'
);Error:
Cannot use positional argument after named argumentSummary #
- Use named arguments to pass arguments to a function based on the parameter names.
- Put the named arguments after the positional arguments in function calls.
Thank you for your feedback!