Bird
0
0

What will be the output of this PHP script when the page is refreshed for the second time?

medium📝 Predict Output Q4 of 15
PHP - Request Lifecycle
What will be the output of this PHP script when the page is refreshed for the second time?
<?php
$visits = 0;
$visits += 1;
echo $visits;
?>
AError
B2
C0
D1
Step-by-Step Solution
Solution:
  1. Step 1: Understand variable initialization

    The variable $visits is set to 0 every time the script runs.
  2. Step 2: Increment operation

    $visits is incremented by 1, so it becomes 1 on each request.
  3. Final Answer:

    1 -> Option D
  4. Quick Check:

    Variable resets on each load, so output is always 1 [OK]
Quick Trick: Variables reset each request unless stored externally [OK]
Common Mistakes:
  • Assuming variable keeps incrementing across requests
  • Expecting output to be 2 or more without session or storage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes