📖 Scenario: You are learning how PHP handles web requests. Each time a user visits a PHP page, the server creates a new process to run your PHP code from the start. This means variables and data do not stay between requests unless saved outside the script.
🎯 Goal: Build a simple PHP script that shows how variables reset on each request by counting visits using a session.
📋 What You'll Learn
Create a PHP script with a variable to count visits
Use a session variable to store the visit count
Increment the visit count on each request
Display the current visit count
💡 Why This Matters
🌍 Real World
Websites use PHP sessions to remember users and keep data like login status or shopping carts between page visits.
💼 Career
Understanding PHP's process model and sessions is essential for backend web developers working with PHP to build dynamic, user-friendly websites.
Progress0 / 4 steps
1
Create a PHP script with a visit count variable
Create a PHP file and write code to start a session with session_start(). Then create a variable called visitCount and set it to 0.
PHP
Hint
Use session_start() at the top to enable sessions. Then create $visitCount and set it to 0.
2
Add session variable to store visit count
Add code to check if $_SESSION['visitCount'] exists. If it does, set $visitCount to $_SESSION['visitCount']. Otherwise, keep $visitCount as 0.
PHP
Hint
Use isset() to check if the session variable exists before using it.
3
Increment the visit count and save it in session
Increase $visitCount by 1. Then save the new value back to $_SESSION['visitCount'].
PHP
Hint
Use $visitCount++ to add one, then store it in the session.
4
Display the current visit count
Write a print statement to show the text: "You have visited this page X times." where X is the value of $visitCount.
PHP
Hint
Use print() with double quotes and include $visitCount inside the string.
Practice
(1/5)
1. What happens to PHP variables after a web request finishes?
easy
A. They become global variables accessible by all users
B. They keep their values for the next request automatically
C. They are stored permanently in the server memory
D. They are reset and lost unless saved externally
Solution
Step 1: Understand PHP request lifecycle
PHP starts fresh for each request, so variables exist only during that request.
Step 2: Identify variable persistence
Variables reset after request ends unless saved in sessions or databases.
Final Answer:
They are reset and lost unless saved externally -> Option D
Quick Check:
PHP variables reset each request = D [OK]
Hint: Remember: PHP starts fresh every request, no variable memory [OK]
Common Mistakes:
Thinking variables persist automatically between requests
Confusing PHP with long-running server processes
Assuming global variables keep values across users
2. Which PHP code snippet correctly shows the start of a new request process?
easy
A.
B.
C.
D.
Solution
Step 1: Analyze variable initialization
In a new request, variables start fresh; $count = 0 sets it properly.
Step 2: Check other options for persistence
Options A, B, D try to increment without initialization or rely on static/session which need extra setup.
Final Answer:
<?php $count = 0; echo $count; ?> -> Option C
Quick Check:
New request means fresh variables = C [OK]
Hint: Fresh request means variables start from scratch [OK]
Common Mistakes:
Assuming static variables keep value across requests
Using session without starting it
Incrementing uninitialized variables
3. What will be the output of this PHP code when run twice in separate requests?