0
0
PHPprogramming~10 mins

Why variables do not persist between requests in PHP - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why variables do not persist between requests
Start Request 1
Set variable $x = 5
End Request 1 - Script stops
Start Request 2
Variable $x does NOT exist
Set $x again or error
End Request 2
Each web request runs a new PHP script instance, so variables set in one request are lost when it ends.
Execution Sample
PHP
<?php
$x = 5;
echo $x;
?>
This code sets $x to 5 and prints it during one request.
Execution Table
StepActionVariable $xOutput
1Start Request 1undefined
2Set $x = 55
3Print $x55
4End Request 15 (lost after script ends)
5Start Request 2undefined
6Try to print $xundefinedError or empty
7Set $x = 1010
8Print $x1010
9End Request 210 (lost after script ends)
💡 Each request ends and clears all variables; no persistence between requests.
Variable Tracker
VariableStart Request 1After Step 2After Step 4Start Request 2After Step 7After Step 9
$xundefined5lostundefined10lost
Key Moments - 2 Insights
Why does $x lose its value after the first request ends?
Because PHP runs each request as a new script instance, all variables are cleared when the script finishes (see execution_table step 4).
Can variables keep their values automatically between requests?
No, variables do not persist automatically; you must use sessions, cookies, or databases to save data between requests.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of $x at the start of Request 2?
Aundefined
B5
C10
DError
💡 Hint
Check execution_table row 5 'Start Request 2' shows $x as undefined.
At which step does the variable $x get lost after being set?
AStep 6
BStep 3
CStep 4
DStep 7
💡 Hint
Look at execution_table step 4 where the request ends and variables are lost.
If you want $x to keep its value between requests, what should you do?
ADeclare $x as global
BUse sessions or cookies
CSet $x again in each request
DNothing, it persists automatically
💡 Hint
Refer to key_moments about persistence methods beyond normal variables.
Concept Snapshot
PHP variables exist only during one request.
When the script ends, all variables are cleared.
Each new request starts fresh with no previous variables.
To keep data between requests, use sessions, cookies, or databases.
Variables do NOT persist automatically across requests.
Full Transcript
In PHP, each web request runs the script from start to end. Variables like $x exist only during that request. When the request finishes, the script stops and all variables are lost. The next request starts fresh with no memory of previous variables. This is why $x set in one request is undefined in the next. To keep data between requests, you must use special storage like sessions or cookies. This example shows $x set to 5 in the first request and lost after it ends. In the second request, $x is undefined until set again. This behavior is normal and important to understand for web programming.