0
0
PHPprogramming~20 mins

Return values in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Return Values Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this PHP function call?

Consider the following PHP code. What will be printed when foo() is called?

PHP
<?php
function foo() {
    return 5 + 3;
}
echo foo();
AError: Undefined function foo
B53
C8
D5 + 3
Attempts:
2 left
💡 Hint

Remember that return sends a value back from the function.

Predict Output
intermediate
2:00remaining
What does this function return?

Look at this PHP function. What value does it return when called with bar(4)?

PHP
<?php
function bar($x) {
    if ($x > 5) {
        return $x * 2;
    }
    return $x + 2;
}
$result = bar(4);
echo $result;
A10
B8
CError: Missing return statement
D6
Attempts:
2 left
💡 Hint

Check which return line runs for the input 4.

🔧 Debug
advanced
2:00remaining
What error does this code produce?

Examine this PHP code. What error will it cause when run?

PHP
<?php
function baz() {
    echo "Hello";
}
$result = baz();
echo $result;
AWarning: Trying to echo non-scalar value
BNo error, outputs 'Hello'
CNotice: Trying to echo null value
DNotice: Undefined variable $result
Attempts:
2 left
💡 Hint

Consider what the function baz() returns by default.

Predict Output
advanced
2:00remaining
What is the output of this recursive function?

What will this PHP code print?

PHP
<?php
function countdown($n) {
    if ($n <= 0) {
        return "Done!";
    }
    return $n . " " . countdown($n - 1);
}
echo countdown(3);
A3 2 1 Done!
B3 2 1
CDone! 1 2 3
D3 2 1 0 Done!
Attempts:
2 left
💡 Hint

Trace the function calls and how the strings join.

🧠 Conceptual
expert
2:00remaining
What is the value of $result after this code runs?

Consider this PHP code snippet. What is the value of $result after execution?

PHP
<?php
function test() {
    return;
    return 10;
}
$result = test();
Anull
BError: unreachable code
C0
D10
Attempts:
2 left
💡 Hint

What happens when a function returns without a value?