Named arguments let you give values to function parameters by name. This makes your code easier to read and understand.
Named arguments in PHP
functionName(parameterName: value, anotherParameter: value);
You write the parameter name, then a colon, then the value.
You can mix named arguments with regular positional arguments, but positional ones must come first.
<?php function greet(string $name, string $greeting) { echo "$greeting, $name!"; } greet(name: "Alice", greeting: "Hello");
<?php function greet(string $name, string $greeting) { echo "$greeting, $name!"; } greet(greeting: "Hi", name: "Bob");
<?php function setUser(string $name, int $age = 30) { echo "$name is $age years old."; } setUser(name: "Carol");
This program shows how to call a function with named arguments in any order and how to skip optional parameters.
<?php function bookInfo(string $title, string $author, int $year = 2020) { echo "Title: $title\nAuthor: $author\nYear: $year\n"; } // Using named arguments in any order bookInfo(author: "Jane Austen", title: "Pride and Prejudice", year: 1813); // Skipping optional argument bookInfo(title: "1984", author: "George Orwell");
Named arguments were introduced in PHP 8.0.
Positional arguments must come before named arguments in a function call.
Using named arguments improves code readability, especially with many parameters.
Named arguments let you specify function parameters by name.
You can change the order of arguments when calling functions.
They help skip optional parameters and make code easier to read.
Practice
What is the main benefit of using named arguments in PHP functions?
Solution
Step 1: Understand named arguments concept
Named arguments let you specify function parameters by their names instead of position.Step 2: Identify the benefit
This allows changing the order of arguments and skipping optional ones, improving readability.Final Answer:
You can specify arguments by name and change their order. -> Option CQuick Check:
Named arguments = specify by name and reorder [OK]
- Thinking named arguments speed up execution
- Believing all arguments must be given in order
- Confusing named arguments with function definitions
Which of the following is the correct syntax to call a function greet with named arguments name and age?
function greet($name, $age) { return "Hello $name, you are $age."; }Solution
Step 1: Recall named argument syntax in PHP
Named arguments use the syntaxparamName: valuewhen calling functions.Step 2: Check each option
greet(name: "Alice", age: 30); usesname: "Alice"andage: 30, which is correct syntax.Final Answer:
greet(name: "Alice", age: 30); -> Option DQuick Check:
Named argument syntax = paramName: value [OK]
- Using => instead of : for named arguments
- Using = instead of : in calls
- Mixing positional and named arguments incorrectly
What will be the output of the following PHP code?
function describe($color, $shape) {
return "A $color $shape.";
}
echo describe(shape: "circle", color: "red");Solution
Step 1: Understand named argument order
Named arguments allow changing the order of parameters when calling the function.Step 2: Substitute values in function
Here,shapeis "circle" andcoloris "red". The return string is "A $color $shape.", so it becomes "A red circle.".Final Answer:
A red circle. -> Option AQuick Check:
Named arguments reorder parameters = "A red circle." [OK]
- Assuming order must match function definition
- Expecting syntax error from reordered named arguments
- Mixing up parameter names and values
Identify the error in the following PHP code using named arguments:
function multiply($x, $y) {
return $x * $y;
}
echo multiply(y: 5, 3);Solution
Step 1: Check argument order rules
In PHP, positional arguments must come before named arguments when calling a function.Step 2: Analyze the call
The call uses a named argumenty: 5first, then a positional argument3, which is invalid syntax.Final Answer:
Positional argument after named argument causes error. -> Option BQuick Check:
Positional args must precede named args [OK]
- Placing positional arguments after named arguments
- Forgetting colon in named argument
- Assuming named arguments can be mixed in any order
Given the function below, which call correctly uses named arguments to skip the optional country parameter and specify city only?
function locationInfo(string $city, string $country = "USA") {
return "$city, $country";
}Solution
Step 1: Understand optional parameters and named arguments
Thecountryparameter has a default value, so it can be skipped.Step 2: Identify call that skips
locationInfo(city: "Boston"); callscountryand setscitylocationInfo(city: "Boston"), skippingcountryand using default "USA".Final Answer:
locationInfo(city: "Boston"); -> Option AQuick Check:
Skip optional params by omitting them in named args [OK]
- Trying to skip required parameters
- Providing positional argument after named argument
- Specifying optional parameter unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
