0
0
PHPprogramming~5 mins

PHP process model per request

Choose your learning style9 modes available
Introduction

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 request
echo "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.