Bird
0
0

What will be the output of the following PHP code?

medium📝 Predict Output Q13 of 15
PHP - Sessions and Cookies
What will be the output of the following PHP code?
session_start();
$_SESSION['count'] = 1;
$_SESSION['count']++;
echo $_SESSION['count'];
A1
B0
CError
D2
Step-by-Step Solution
Solution:
  1. Step 1: Analyze session variable initialization and increment

    The code sets $_SESSION['count'] to 1, then increments it by 1 using ++.
  2. Step 2: Determine the final value of $_SESSION['count']

    After increment, the value becomes 2, which is then printed by echo.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    1 incremented by 1 = 2 [OK]
Quick Trick: Increment session variable with ++ after setting it [OK]
Common Mistakes:
  • Forgetting to call session_start() before using $_SESSION
  • Assuming $_SESSION['count'] stays 1 after ++
  • Expecting an error due to session usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes