0
0
PHPprogramming~20 mins

Parameters and arguments in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Parameters and Arguments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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();
?>
ASyntax error
BHello, !
CHello, $name!
DHello, Guest!
Attempts:
2 left
💡 Hint
Check what happens when no argument is passed to a function with a default parameter.
Predict Output
intermediate
2: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);
?>
A6
B15
C10
DError: Missing argument
Attempts:
2 left
💡 Hint
The second argument is provided, so default is ignored.
Predict Output
advanced
2: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;
?>
A11
B10
C1
DError: Cannot pass by reference
Attempts:
2 left
💡 Hint
The function parameter is passed by reference, so changes affect the original variable.
Predict Output
advanced
2: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);
?>
A10
B1,2,3,4
CArray
DError: Unexpected ... operator
Attempts:
2 left
💡 Hint
The function uses a variable-length argument list to sum all numbers.
🧠 Conceptual
expert
2: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);
?>
AWarning: Missing argument for parameter 2
BOutput: 10
CFatal error: Uncaught ArgumentCountError
DNo error, outputs 0
Attempts:
2 left
💡 Hint
PHP 7+ throws a specific error when required arguments are missing.