Every PHP web page sits inside the HTTP request–response cycle. Understanding that cycle explains superglobals, sessions, headers, and why PHP cannot "push" data to a closed tab without another request or WebSockets elsewhere.
Steps in order
- Client request — browser or API client sends method (GET/POST), URL, headers, optional body
- Web server — routes the URL to a
.phpfile or front controller (index.php) - PHP bootstrap — autoloaders, config, session start (if used)
- Your logic — read input, validate, query DB, choose view
- Response — status line, headers (Content-Type, Set-Cookie), body (HTML/JSON)
- Process ends — unless using long-running workers (RoadRunner, FrankenPHP), the PHP process typically exits
Statelessness
HTTP is stateless: each request is independent. PHP remembers users via sessions (server storage + session cookie) or JWT/tokens in API designs. Do not assume variables from request A exist in request B.
Important interview questions and answers
- Q: What happens between clicking a link and seeing HTML?
A: DNS → TCP/TLS → HTTP request → server runs PHP → HTML response → browser parses and renders. - Q: Why is HTTP called stateless?
A: The protocol does not inherently tie requests together; apps add cookies, sessions, or tokens to simulate state. - Q: Where does PHP run in this chain?
A: On the server after the web server receives the request and before the response is sent to the client.
Self-check
- What carries form data in a POST request—the URL or the body?
- Why must sessions use cookies or equivalent identifiers?
Interview prep
- Why is HTTP stateless?
Each request is independent; PHP uses sessions, cookies, or tokens to remember users across requests.