Lesson 62 of 100
Functions15 min5 checksPHP 8.5

Call Functions with Named Arguments

Learn how PHP 8 named arguments let you skip defaults, ignore parameter order, and keep calls readable, then apply them in a small exercise.

01 · THE IDEAREAD → TRY → LEARN

Why names help

A call like makeYogurt("cup", "mango", "skyr") works, but a reader must remember what each position means. Since PHP 8.0 you can write the parameter name before the value, followed by a colon. The call documents itself, and you can skip optional parameters you do not care about.

The name is the one in the function declaration, without the $. It must be written literally. You cannot put a variable in that spot.

Skipping defaults

Before named arguments, changing only the last optional parameter forced you to repeat every default before it. Now you pass just the one you want:

Example
<?php
function makeYogurt($container = "bowl", $flavour = "raspberry", $style = "Greek")
{
    return "Making a $container of $flavour $style yogurt.\n";
}

echo makeYogurt(style: "natural");
Output
Making a bowl of raspberry natural yogurt.

The other two parameters keep their defaults. Because the arguments are matched by name, their order in the call does not matter.

Mixing positional and named arguments

You may combine both styles, but positional arguments must come first. After the first named argument, everything else must be named too. Each parameter can receive a value only once. Giving the same parameter both a positional and a named value, or naming it twice, throws an Error.

Example
<?php
function makeYogurt($container = "bowl", $flavour = "raspberry", $style = "Greek")
{
    return "Making a $container of $flavour $style yogurt.\n";
}

echo makeYogurt("cup", style: "skyr");
echo makeYogurt(style: "skyr", flavour: "mango");
echo makeYogurt(flavour: null);
Output
Making a cup of raspberry skyr yogurt.
Making a bowl of mango skyr yogurt.
Making a bowl of  Greek yogurt.

The last call shows an important rule. Passing null is not the same as leaving an argument out. A default is used only when no argument is given for that parameter, so flavour: null really sends null, which prints as an empty string here.

Named arguments and unpacking

An array with string keys can be unpacked into named arguments with .... The keys must match parameter names. This is handy when you build options step by step. Since PHP 8.1 you can also add named arguments after an unpacked array, provided they do not repeat a name that was already supplied.

Example
<?php
function makeYogurt($container = "bowl", $flavour = "raspberry", $style = "Greek")
{
    return "Making a $container of $flavour $style yogurt.\n";
}

$options = ['style' => 'natural', 'container' => 'cup'];
echo makeYogurt(...$options);

$options = ['flavour' => 'mango'];
echo makeYogurt(...$options, style: 'skyr');
Output
Making a cup of raspberry natural yogurt.
Making a bowl of mango skyr yogurt.

This pattern is the key to the exercise: build an array containing only the options you actually have, then unpack it. Omitted keys let the defaults apply.

Practical guidance

  • Use named arguments when a call has several optional parameters or boolean flags.
  • Treat parameter names as part of a function's public interface. Renaming a parameter can break callers that use names.
  • Keep required parameters first in your own declarations, and give optional ones defaults.
  • Remember that null is a real value, not a request for the default.

Exercise contract

The starter already contains makeYogurt() with the defaults shown above, except that it returns its text without a trailing newline. Implement yogurtOrder(string $style, ?string $flavour = null, ?string $container = null): string.

  • Always send $style to makeYogurt().
  • Send $flavour only when it is not null.
  • Send $container only when it is not null.
  • Return the string produced by makeYogurt().

For example, yogurtOrder("Greek", null, "cup") returns Making a cup of raspberry Greek yogurt. An empty style is allowed and must not be treated as missing. The tests check the returned strings, so do not print anything.

Recap

Named arguments let you target a parameter by its declared name, skip defaults, and reorder values. Positional arguments come first, a parameter cannot be given twice, and null does not trigger a default. String-keyed arrays can be unpacked into named arguments, which makes optional-value handling concise.

Before you start

Functions · Arrays · Types

You’ll practice
  • Use declared parameter names and defaults without reordering errors
  • Implement and test a deterministic PHP 8.4 function
Your task

Make yogurtOrder() pass all 5 checks. Run your code as often as you like, then check your answer.

exercise.phpSaved in this browserPHP 8.5
CONSOLE & CHECKS● Ready

Ready when you are

Your output will appear here.
Make it work. Then make it pass.

PHP loads on your first run. Each check starts fresh. Your code stays in this browser; clearing site data removes your progress. A check or the task looks wrong? Report a problem with this exercise.