Bird
0
0

What will be the output when this PHP code is executed twice in separate requests?

medium📝 Predict Output Q4 of 15
PHP - Request Lifecycle
What will be the output when this PHP code is executed twice in separate requests?
<?php
$counter = 5;
$counter++;
echo $counter;
?>
A5 on first request, 6 on second request
B6 on first request, 7 on second request
C6 on both requests
DError because $counter is undefined on second request
Step-by-Step Solution
Solution:
  1. Step 1: Analyze variable initialization

    $counter is set to 5 at the start of each request.
  2. Step 2: Increment operation

    $counter++ increases it to 6 before echoing.
  3. Step 3: Request isolation

    Each request resets $counter to 5, so output is always 6.
  4. Final Answer:

    6 on both requests -> Option C
  5. Quick Check:

    Variables reset each request, so output repeats [OK]
Quick Trick: Variables reset each request, output repeats [OK]
Common Mistakes:
  • Assuming $counter increments persist between requests
  • Confusing script execution with persistent storage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes