Challenge - 5 Problems
Parameters and Arguments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of function with default parameter
What is the output of this PHP code?
PHP
<?php function greet($name = "Guest") { echo "Hello, $name!"; } greet(); ?>
Attempts:
2 left
💡 Hint
Check what happens when no argument is passed to a function with a default parameter.
✗ Incorrect
The function greet has a default parameter value 'Guest'. When called without arguments, it uses this default and prints 'Hello, Guest!'.
❓ Predict Output
intermediate2:00remaining
Output with passed argument overriding default
What will this PHP code output?
PHP
<?php function multiply($a, $b = 2) { return $a * $b; } echo multiply(5, 3); ?>
Attempts:
2 left
💡 Hint
The second argument is provided, so default is ignored.
✗ Incorrect
The function multiply multiplies $a and $b. Here, $a=5 and $b=3 (passed), so output is 15.
❓ Predict Output
advanced2:00remaining
Output of function with reference parameter
What is the output of this PHP code?
PHP
<?php function addOne(&$num) { $num += 1; } $value = 10; addOne($value); echo $value; ?>
Attempts:
2 left
💡 Hint
The function parameter is passed by reference, so changes affect the original variable.
✗ Incorrect
The function addOne increases the value of $num by 1. Since $num is passed by reference, $value changes from 10 to 11.
❓ Predict Output
advanced2:00remaining
Output of function with variable-length argument list
What will this PHP code output?
PHP
<?php function sumAll(...$numbers) { return array_sum($numbers); } echo sumAll(1, 2, 3, 4); ?>
Attempts:
2 left
💡 Hint
The function uses a variable-length argument list to sum all numbers.
✗ Incorrect
The function sumAll collects all arguments into an array and sums them. 1+2+3+4=10.
🧠 Conceptual
expert2:00remaining
Identify the error in function call with missing argument
What error will this PHP code produce?
PHP
<?php function divide($x, $y) { return $x / $y; } echo divide(10); ?>
Attempts:
2 left
💡 Hint
PHP 7+ throws a specific error when required arguments are missing.
✗ Incorrect
Calling divide with only one argument causes a fatal ArgumentCountError because the second required parameter is missing.