Bird
0
0

What will this PHP code output?

medium📝 Predict Output Q5 of 15
PHP - Functions
What will this PHP code output?
$x = 5;
function foo() {
$x = 10;
global $x;
echo $x;
}
foo();
AError
B10
C5
DNothing
Step-by-Step Solution
Solution:
  1. Step 1: Understand variable declaration order

    In PHP, declaring a local variable $x = 10; before declaring global $x; causes a fatal error because the same variable cannot be both local and global in the same scope.
  2. Step 2: Recognize error due to conflicting declarations

    The code will produce a fatal error due to this conflict.
  3. Final Answer:

    Error -> Option A
  4. Quick Check:

    Declaring local variable before global causes error [OK]
Quick Trick: Declaring local variable before global causes error [OK]
Common Mistakes:
  • Expecting local variable to print
  • Thinking global and local coexist
  • Assuming code runs without error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes