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:
function makeYogurt($container = "bowl", $flavour = "raspberry", $style = "Greek")
{
return "Making a $container of $flavour $style yogurt.\n";
}
echo makeYogurt(style: "natural");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.
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);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.
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');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
nullis 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
$styletomakeYogurt(). - Send
$flavouronly when it is notnull. - Send
$containeronly when it is notnull. - 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.
Functions · Arrays · Types
- Use declared parameter names and defaults without reordering errors
- Implement and test a deterministic PHP 8.4 function