PHP runs your code every time a user asks for a page. It starts fresh each time to keep things simple and safe.
When a website needs to show a new page for each visitor.
When you want to make sure each user gets a clean start without leftover data.
When handling form submissions where each request is separate.
When building simple web apps that don't keep running all the time.
When you want to avoid complex memory management between users.
Syntax
PHP
<?php
// PHP script runs from start to end for each request// No persistent variables between requests
?>
PHP scripts start fresh on every request, so no data stays in memory after the script ends.
Each request is handled independently, like a new conversation every time.
Examples
This script runs and prints a message each time someone visits the page.
PHP
<?php
// First requestecho"Hello, visitor!";
?>
Even if you refresh the page, $count starts at 0 again because PHP resets on each request.
PHP
<?php
// Variables do not keep values between requests
$count = 0;
$count++;
echo $count; // Always prints 1
?>
Sample Program
This program counts how many times you visit the page using a session. The PHP process itself starts fresh each time, but sessions let you remember data between requests.
PHP
<?php
// Simple PHP script showing process per request
session_start();
if (!isset($_SESSION['visits'])) {
$_SESSION['visits'] = 1;
} else {
$_SESSION['visits']++;
}
echo"You have visited this page " . $_SESSION['visits'] . " times.";
?>
OutputSuccess
Important Notes
PHP does not keep variables or data in memory between requests unless you use sessions or external storage.
Each request is like a new start, so you must save important data outside the script if you want to keep it.
Summary
PHP runs a new process for every request, starting fresh each time.
Variables reset on every request unless stored in sessions or databases.
This model keeps web apps simple and secure by isolating each user's request.
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?