0
0
PHPprogramming~5 mins

Named arguments in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What are named arguments in PHP?
Named arguments allow you to pass values to a function by specifying the parameter name, not just the order. This makes code clearer and lets you skip optional parameters.
Click to reveal answer
beginner
How do named arguments improve function calls?
They improve readability by showing which value goes to which parameter. They also let you pass arguments in any order and skip optional ones.
Click to reveal answer
beginner
Show an example of calling a PHP function with named arguments.
Example:<br><code>function greet(string $name, string $greeting = "Hello") {<br>  echo "$greeting, $name!";<br>}<br>greet(name: "Alice", greeting: "Hi");</code>
Click to reveal answer
intermediate
Can you mix positional and named arguments in PHP function calls?
Yes, but positional arguments must come first. After that, you can use named arguments in any order.
Click to reveal answer
intermediate
What happens if you use a named argument that does not exist in the function?
PHP will throw a fatal error because the function does not have a parameter with that name.
Click to reveal answer
Which of the following is a correct way to call a function with named arguments in PHP?
Afoo(bar: 10, baz: 20);
Bfoo(10: bar, 20: baz);
Cfoo(bar = 10, baz = 20);
Dfoo(10, 20);
Can you skip optional parameters when using named arguments in PHP?
AOnly if the function has no default values.
BNo, you must pass all parameters in order.
COnly if the parameters are at the end.
DYes, by naming only the parameters you want to pass.
What is the rule about mixing positional and named arguments in PHP?
APositional arguments must come before named arguments.
BNamed arguments must come before positional arguments.
CYou cannot mix positional and named arguments.
DOrder does not matter.
What error occurs if you use a named argument that does not match any parameter?
AThe function uses the default value.
BA fatal error is thrown.
CThe argument is ignored.
DA warning is shown but code runs.
Which PHP version introduced named arguments?
APHP 7.0
BPHP 7.4
CPHP 8.0
DPHP 5.6
Explain how named arguments work in PHP and why they are useful.
Think about how naming parameters helps understand code better.
You got /4 concepts.
    Describe the rules for mixing positional and named arguments in PHP function calls.
    Remember the order and naming rules.
    You got /4 concepts.