Bird
0
0

You want to count how many times a function is called using a global variable. Which code correctly updates the global counter inside the function?

hard📝 Application Q8 of 15
PHP - Functions
You want to count how many times a function is called using a global variable. Which code correctly updates the global counter inside the function?
Aglobal $counter = 0; $counter++;
B$counter++;
Cglobal $counter; $counter++;
Dglobal $counter; $counter = 0;
Step-by-Step Solution
Solution:
  1. Step 1: Use global to access global variable

    Inside the function, declare global $counter; to access the global variable.
  2. Step 2: Increment the global counter

    Then increment it with $counter++; to count calls correctly.
  3. Final Answer:

    global $counter; $counter++; -> Option C
  4. Quick Check:

    Use global then increment to update global counter [OK]
Quick Trick: Declare global before modifying global variable inside function [OK]
Common Mistakes:
  • Incrementing without global declaration
  • Reinitializing global variable inside function
  • Using incorrect global syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes