0
0
PHPprogramming~20 mins

Function declaration and calling in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a function with default parameter
What is the output of this PHP code?
PHP
<?php
function greet($name = "Guest") {
    return "Hello, $name!";
}
echo greet();
?>
AHello, name!
BHello, !
CHello, Guest!
DSyntax Error
Attempts:
2 left
💡 Hint
Check the default value of the parameter in the function.
Predict Output
intermediate
2:00remaining
Return value from a function call
What will be the output of this PHP code?
PHP
<?php
function add($a, $b) {
    return $a + $b;
}
$result = add(5, 3);
echo $result;
?>
A5 + 3
B53
CError: Undefined function
D8
Attempts:
2 left
💡 Hint
The function returns the sum of two numbers.
🔧 Debug
advanced
2:00remaining
Identify the error in function call
What error will this PHP code produce?
PHP
<?php
function multiply($x, $y) {
    return $x * $y;
}
echo multiply(4);
?>
AWarning: Missing argument 2 for multiply()
BFatal error: Call to undefined function multiply()
COutput: 4
DNo output, code runs fine
Attempts:
2 left
💡 Hint
Check how many arguments the function expects and how many are given.
Predict Output
advanced
2:00remaining
Output of recursive function
What is the output of this PHP code?
PHP
<?php
function factorial($n) {
    if ($n <= 1) {
        return 1;
    } else {
        return $n * factorial($n - 1);
    }
}
echo factorial(4);
?>
A24
B10
CError: Maximum function nesting level reached
D4
Attempts:
2 left
💡 Hint
Factorial of 4 is 4*3*2*1.
🧠 Conceptual
expert
2:00remaining
Function variable scope behavior
What will be the output of this PHP code?
PHP
<?php
$x = 10;
function test() {
    $x = 5;
    echo $x . ' ';
}
test();
echo $x;
?>
A10 10
B5 10
C5 5
D10 5
Attempts:
2 left
💡 Hint
Variables inside functions have their own scope unless declared global.