Bird
0
0

What will be the output of the following PHP code?

medium📝 Predict Output Q13 of 15
PHP - Functions
What will be the output of the following PHP code?
$x = 5;
function test() {
    $x = 10;
    echo $x;
}
test();
echo $x;
A10 5
B5 10
C10 10
D5 5
Step-by-Step Solution
Solution:
  1. Step 1: Analyze variable scope inside the function

    Inside test(), $x is assigned 10 locally, so echo $x; prints 10.
  2. Step 2: Analyze variable outside the function

    Outside the function, $x remains 5 because the local $x inside the function does not affect the global $x.
  3. Final Answer:

    10 5 -> Option A
  4. Quick Check:

    Local $x = 10, global $x = 5 [OK]
Quick Trick: Local variables inside functions do not change globals [OK]
Common Mistakes:
  • Assuming function changes global variable without 'global' keyword
  • Confusing output order
  • Thinking variables inside functions share the same memory as globals

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes