Bird
0
0

You want to count how many times a user visits a page using PHP. Which approach correctly keeps the count between requests?

hard📝 Application Q15 of 15
PHP - Request Lifecycle
You want to count how many times a user visits a page using PHP. Which approach correctly keeps the count between requests?
ADeclare a global variable and increment it on each request
BUse a session variable with <code>session_start()</code> and increment it
CUse a local variable inside the script and increment it
DStore the count in a variable inside the HTML code
Step-by-Step Solution
Solution:
  1. Step 1: Understand variable lifetime

    Local and global PHP variables reset each request, so they can't keep count.
  2. Step 2: Use sessions for persistence

    Sessions store data on the server between requests, so using session_start() and a session variable keeps count.
  3. Final Answer:

    Use a session variable with session_start() and increment it -> Option B
  4. Quick Check:

    Sessions keep data between requests = A [OK]
Quick Trick: Use sessions to save data across page loads [OK]
Common Mistakes:
  • Thinking global variables persist across requests
  • Trying to store PHP variables in HTML
  • Not starting session before using session variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes