0
0
PHPprogramming~20 mins

Named arguments in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Named Arguments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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');
?>
AHi, Alice!
BSyntaxError
CHello, Alice!
DHi, greeting!
Attempts:
2 left
💡 Hint
Named arguments allow you to specify parameters by name in any order.
Predict Output
intermediate
2: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);
?>
A17
BSyntaxError
C32
D15
Attempts:
2 left
💡 Hint
Remember the order of operations and default values.
Predict Output
advanced
2: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);
?>
AFatal error
B1
CWarning: Missing argument for variadic parameter
D10
Attempts:
2 left
💡 Hint
Variadic parameters cannot be passed as named arguments as arrays directly.
Predict Output
advanced
2: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');
?>
A15
BTypeError
CWarning: Invalid argument
D5three
Attempts:
2 left
💡 Hint
PHP 7+ enforces type declarations strictly by default.
🧠 Conceptual
expert
2: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);
?>
A12
B7
CFatal error
D60
Attempts:
2 left
💡 Hint
Check method signatures and parameter order in inheritance.