Bird
0
0

Identify the bug in this PHP function:

medium📝 Debug Q7 of 15
PHP - Functions
Identify the bug in this PHP function:
function getValue() {
  echo 10;
  return 20;
}
$value = getValue();
echo $value;
AFunction returns 10 instead of 20
BEcho inside function outputs before return
CVariable $value is not assigned
DFunction has no return statement
Step-by-Step Solution
Solution:
  1. Step 1: Understand echo and return behavior

    Echo prints immediately; return sends value back but does not print.
  2. Step 2: Trace output and assignment

    Function prints 10, then returns 20 which is assigned to $value and echoed.
  3. Final Answer:

    Echo inside function outputs before return -> Option B
  4. Quick Check:

    Echo prints immediately; return sends value = B [OK]
Quick Trick: Echo prints now; return sends value back [OK]
Common Mistakes:
  • Thinking return prints value
  • Assuming echo assigns value
  • Believing variable is unassigned

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes