Challenge - 5 Problems
Named Arguments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of function call with named arguments
What is the output of this PHP code using named arguments?
PHP
<?php function greet($name, $greeting = 'Hello') { return "$greeting, $name!"; } echo greet(greeting: 'Hi', name: 'Alice'); ?>
Attempts:
2 left
💡 Hint
Named arguments allow you to specify parameters by name in any order.
✗ Incorrect
The function greet is called with named arguments greeting: 'Hi' and name: 'Alice'. This overrides the default greeting and prints 'Hi, Alice!'.
❓ Predict Output
intermediate2:00remaining
Named arguments with default values
What will this PHP code output when using named arguments?
PHP
<?php function calculate($x, $y = 10, $z = 5) { return $x + $y * $z; } echo calculate(z: 3, x: 2); ?>
Attempts:
2 left
💡 Hint
Remember the order of operations and default values.
✗ Incorrect
Named arguments assign x=2 and z=3. y uses default 10. So calculation is 2 + 10 * 3 = 2 + 30 = 32.
❓ Predict Output
advanced2:00remaining
Named arguments with variadic function
What is the output of this PHP code using named arguments with a variadic function?
PHP
<?php function sum($a, ...$numbers) { return $a + array_sum($numbers); } echo sum(numbers: [2, 3, 4], a: 1); ?>
Attempts:
2 left
💡 Hint
Variadic parameters cannot be passed as named arguments as arrays directly.
✗ Incorrect
Passing an array to a variadic parameter using named arguments causes a fatal error because PHP expects separate arguments, not an array.
❓ Predict Output
advanced2:00remaining
Named arguments with type mismatch
What error does this PHP code produce when calling a function with named arguments?
PHP
<?php function multiply(int $a, int $b) { return $a * $b; } echo multiply(a: 5, b: 'three'); ?>
Attempts:
2 left
💡 Hint
PHP 7+ enforces type declarations strictly by default.
✗ Incorrect
Passing a string 'three' to an int parameter causes a TypeError in PHP 7+ with strict typing.
🧠 Conceptual
expert2:00remaining
Behavior of named arguments with inheritance
Consider this PHP code with inheritance and named arguments. What is the output?
PHP
<?php class Base { public function display($x, $y = 10) { echo $x + $y; } } class Child extends Base { public function display($x, $y = 20) { echo $x * $y; } } $obj = new Child(); $obj->display(x: 3, y: 4); ?>
Attempts:
2 left
💡 Hint
Check method signatures and parameter order in inheritance.
✗ Incorrect
Child class method display has parameters in the same order as Base but with different default values, which is allowed. So output is 3 * 4 = 12.