This topic helps you understand how PHP works differently from long-running servers like Node.js. It shows why PHP starts fresh for each request, while Node.js keeps running all the time.
Comparison with long-running servers (Node.js) in PHP
<?php // PHP script runs fresh on each request // Node.js server runs continuously ?>
PHP scripts start from zero every time a user visits a page.
Node.js servers keep running and handle many requests without restarting.
<?php // PHP example $count = 0; $count++; echo $count; // Always prints 1 ?>
// Node.js example let count = 0; const http = require('http'); http.createServer((req, res) => { count++; res.end(`Count is ${count}`); }).listen(3000);
This PHP script uses a session to remember the count between requests, simulating persistent data.
<?php // PHP script to show count session_start(); if (!isset($_SESSION['count'])) { $_SESSION['count'] = 0; } $_SESSION['count']++; echo "Count is " . $_SESSION['count']; ?>
PHP resets all variables on each request unless you use sessions or databases.
Node.js can keep variables in memory because it runs continuously.
Using sessions in PHP is a way to keep data between requests, but it is different from a long-running server.
PHP scripts start fresh on every request, so variables reset.
Node.js servers run all the time and keep data in memory between requests.
Sessions or databases help PHP remember data across requests.
Practice
Solution
Step 1: Understand PHP's request model
PHP scripts start fresh on each request, so all variables are reset.Step 2: Understand Node.js server behavior
Node.js servers run continuously, so variables can persist in memory across requests.Final Answer:
PHP resets variables on every request, while Node.js keeps variables in memory between requests. -> Option CQuick Check:
PHP resets, Node.js persists [OK]
- Thinking PHP keeps variables in memory between requests
- Assuming Node.js resets variables like PHP
- Confusing session storage with variable persistence
Solution
Step 1: Identify the correct PHP function to start a session
The correct function issession_start()to begin or resume a session.Step 2: Check the code syntax
usessession_start()correctly; others use invalid function names.Final Answer:
<?php session_start(); $_SESSION['count'] = 1; ?> -> Option AQuick Check:
Use session_start() to begin sessions [OK]
- Using incorrect function names like start_session()
- Forgetting to call session_start() before using $_SESSION
- Assuming sessions start automatically
<?php
session_start();
if (!isset($_SESSION['visits'])) {
$_SESSION['visits'] = 1;
} else {
$_SESSION['visits']++;
}
echo $_SESSION['visits'];
?>
What will be the output on the second request?Solution
Step 1: Analyze session variable initialization
On the first request,$_SESSION['visits']is not set, so it is initialized to 1.Step 2: Analyze second request behavior
On the second request,$_SESSION['visits']exists, so it increments by 1, becoming 2.Final Answer:
2 -> Option BQuick Check:
Session increments visits count correctly [OK]
- Assuming visits resets to 1 every request
- Forgetting to call session_start()
- Expecting an error due to undefined index
<?php
if (!isset($_SESSION['count'])) {
$_SESSION['count'] = 1;
} else {
$_SESSION['count']++;
}
echo $_SESSION['count'];
?>
What is the most likely cause of this problem?Solution
Step 1: Check for session initialization
The code does not callsession_start(), so PHP does not load or save session data.Step 2: Understand session behavior without session_start()
Without starting the session,$_SESSIONis empty each request, so count resets to 1 every time.Final Answer:
The session_start() function is missing, so sessions don't work. -> Option AQuick Check:
Always call session_start() before using $_SESSION [OK]
- Assuming $_SESSION works without session_start()
- Thinking variable reset is due to manual code
- Ignoring session initialization step
Solution
Step 1: Understand PHP's request lifecycle
PHP scripts run fresh on each request, so local variables reset automatically.Step 2: Identify how to keep data in memory across requests
To keep data in memory like Node.js, PHP must run as a long-running process (daemon) and communicate externally.Step 3: Evaluate options
Use a PHP daemon script running continuously and communicate via sockets or shared memory. describes running a PHP daemon with sockets/shared memory, which simulates long-running server behavior.Final Answer:
Use a PHP daemon script running continuously and communicate via sockets or shared memory. -> Option DQuick Check:
Long-running PHP needs daemon + IPC, not per-request scripts [OK]
- Thinking local variables persist across requests
- Confusing cookies with server memory
- Restarting server resets all variables
Ready to prove it?
Pass the test and this pattern moves unknown
