Module 01: Hello World

Learning Objectives

The Request-Response Cycle

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
        

Demo Files

hello.php The simplest possible PHP script - just echoes text.

templating.php Mix PHP with HTML - dynamic content in static pages.

The Simplest PHP Script

<?php
echo "Hello, World!";
?>

That's it! The <?php tag starts PHP mode, echo outputs text, and ?> ends PHP mode.

Tip: For files that are pure PHP (no HTML), you can omit the closing ?> tag. This prevents accidental whitespace issues.

Mixing PHP and HTML

<!DOCTYPE html>
<html>
<body>
    <h1><?php echo "Hello, World!"; ?></h1>
    <p>Current time: <?= date('H:i:s') ?></p>
</body>
</html>

Key Concept: Stateless Execution

PHP runs per-request. Each request starts fresh:

This is why we need sessions to maintain state between requests.

Try This

  1. View hello.php and note what appears in the browser
  2. View templating.php and refresh several times - watch the time change
  3. Right-click → "View Page Source" to see that no PHP code appears in the output
  4. Try modifying the files and refreshing to see changes immediately

Back to Tutorial | Next: Form Handling →