Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q4 of 15
PHP - Functions
What will be the output of this PHP code?
$a = 10;
function test() {
global $a;
$a = 20;
}
test();
echo $a;
A10
BError
C20
DNothing
Step-by-Step Solution
Solution:
  1. Step 1: Analyze global keyword effect

    Inside test(), global $a; links to the global variable $a.
  2. Step 2: Variable modification

    Assigning $a = 20; changes the global $a value to 20.
  3. Final Answer:

    20 -> Option C
  4. Quick Check:

    global modifies global variable = 20 [OK]
Quick Trick: global lets function change global variable value [OK]
Common Mistakes:
  • Expecting original value unchanged
  • Thinking global creates local copy
  • Assuming code causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes