Bird
0
0

What is the problem with this PHP code snippet?

medium📝 Debug Q7 of 15
PHP - Request Lifecycle
What is the problem with this PHP code snippet?
global $score;
$score = 10;
function updateScore() {
global $score;
$score += 5;
}
updateScore();
echo $score;
AUsing global outside function is invalid
BGlobal variables can cause unpredictable bugs
CGlobal keyword inside function is unnecessary
DFunction updateScore() is missing return statement
Step-by-Step Solution
Solution:
  1. Step 1: Analyze use of global keyword

    Using global inside and outside function is valid syntax in PHP.
  2. Step 2: Identify conceptual problem

    Global variables like $score can cause unpredictable bugs due to shared state.
  3. Final Answer:

    Global variables can cause unpredictable bugs -> Option B
  4. Quick Check:

    Global state risk = unpredictable bugs [OK]
Quick Trick: Global variables risk hidden side effects [OK]
Common Mistakes:
  • Thinking global keyword outside function is invalid
  • Believing return statement is required here
  • Assuming global keyword inside function is always wrong

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes