Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q13 of 15
PHP - Request Lifecycle
What will be the output of this PHP code?
$count = 5;
function increment() {
global $count;
$count++;
}
increment();
echo $count;
A5
BNothing is printed
CError: Undefined variable
D6
Step-by-Step Solution
Solution:
  1. Step 1: Understand the global keyword usage

    The function declares global $count; which means it uses the global variable $count defined outside.
  2. Step 2: Trace the code execution

    The global $count starts at 5, the function increments it by 1, so $count becomes 6. Then echo $count; prints 6.
  3. Final Answer:

    6 -> Option D
  4. Quick Check:

    Global increment changes $count to 6 [OK]
Quick Trick: Global keyword lets function modify external variable [OK]
Common Mistakes:
  • Thinking $count inside function is local without global keyword
  • Expecting output 5 because of misunderstanding scope
  • Assuming code causes error due to variable scope

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes