When you request a .php file, here's what happens:
1. Browser sends HTTP request: GET /hello.php HTTP/1.1
2. Web server receives request, sees .php extension
3. Server invokes PHP interpreter to execute the script
4. PHP runs the code, collecting all output (echo, print, HTML outside tags)
5. Server wraps output in HTTP response:
HTTP/1.1 200 OK
Content-Type: text/html
Hello, World!
6. Browser displays the response
hello.php The simplest possible PHP script - just echoes text.
templating.php Mix PHP with HTML - dynamic content in static pages.
<?php echo "Hello, World!"; ?>
That's it! The <?php tag starts PHP mode, echo outputs text, and ?> ends PHP mode.
?> tag. This prevents accidental whitespace issues.
<!DOCTYPE html>
<html>
<body>
<h1><?php echo "Hello, World!"; ?></h1>
<p>Current time: <?= date('H:i:s') ?></p>
</body>
</html>
<?php ?> passes through unchanged<?= ... ?> is shorthand for <?php echo ... ?>PHP runs per-request. Each request starts fresh:
This is why we need sessions to maintain state between requests.