Bird
0
0

Consider this PHP code run twice separately:

medium📝 Predict Output Q5 of 15
PHP - Request Lifecycle
Consider this PHP code run twice separately:
<?php
session_start();
if (!isset($_SESSION['count'])) {
  $_SESSION['count'] = 0;
}
$_SESSION['count']++;
echo $_SESSION['count'];
?>

What will be the output on the second run?
A1
BError
C0
D2
Step-by-Step Solution
Solution:
  1. Step 1: Understand session variable behavior

    Session data persists between runs, so $_SESSION['count'] keeps incrementing.
  2. Step 2: Trace first and second run values

    First run sets count to 1, second run increments to 2 and echoes it.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    Session data persists = 2 [OK]
Quick Trick: Sessions keep data between runs, so count increments [OK]
Common Mistakes:
  • Forgetting to call session_start()
  • Assuming session variables reset each run
  • Confusing session with local variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes