Bird
0
0

Consider this PHP code run twice in a row (two separate page loads):

medium📝 Predict Output Q13 of 15
PHP - Request Lifecycle
Consider this PHP code run twice in a row (two separate page loads):
<?php
$counter = 0;
$counter++;
echo $counter;
?>

What will be the output each time the script runs?
A1 then 1
B0 then 1
C1 then 2
D0 then 0
Step-by-Step Solution
Solution:
  1. Step 1: Analyze variable initialization

    Each time the script runs, $counter is set to 0 at the start.
  2. Step 2: Increment and output

    $counter++ increases $counter to 1, then echo prints 1 each time.
  3. Final Answer:

    1 then 1 -> Option A
  4. Quick Check:

    Variables reset each run, so output is 1 each time [OK]
Quick Trick: Variables reset each run, so counter restarts at 0 [OK]
Common Mistakes:
  • Assuming $counter keeps incrementing across runs
  • Confusing pre-increment and post-increment
  • Thinking echo prints 0 first

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes