Phase 1 · PHP FoundationsModule 1~40 min read

How PHP Powers the Web

Understand PHP's role on the server, set up a modern environment, and run your first web and command-line scripts.

What you'll learn

PHP is a server-side programming language built for the web. In this lesson you'll see exactly where it runs, set up a useful development loop, and create a real dynamic page.

By the end, you'll be able to:

  • Explain the difference between browser code and server-side PHP
  • Trace an HTTP request through a web server and PHP
  • Run PHP from both the command line and a local web server
  • Use PHP tags, statements, variables, echo, and comments
  • Read an error message and use it to find the failing line

Where PHP runs

HTML, CSS, and JavaScript are sent to the browser. PHP is different: the server executes the PHP source first and sends only the resulting response—usually HTML or JSON. Visitors never need PHP installed, and they should never receive your source code.

One request, four steps
1

Browser

Requests /hello.php

2

Web server

Routes the request to PHP

3

PHP

Executes code and builds output

4

Browser

Receives an HTML response

PHP executes on the server before the response reaches the browser.

Key idea

PHP's output becomes the response body. If your script prints HTML, the browser renders a page; if it prints JSON with the right header, it behaves like an API.

Set up a development environment

Install a supported PHP 8 release using the package manager or installer for your operating system. A beginner-friendly all-in-one environment is fine, but learning the two commands below keeps your workflow portable.

Terminal
php --version
php --ini
The first command confirms PHP is available; the second shows its active configuration.

Create a folder for the course, open a terminal there, then start PHP's development server:

Terminal
mkdir php-course
cd php-course
php -S localhost:8000

Development only

PHP's built-in server is excellent for lessons and local prototypes. It is not a production web server and should not be exposed directly to the public internet.

Your first PHP script

Save this as hello.php. Run it with php hello.php in the terminal, or visit http://localhost:8000/hello.php while the local server is running.

hello.php
<?php

$student = "Maya";
$topic = "PHP";

echo "Hello, {$student}!";
echo "<p>Welcome to {$topic}.</p>";

PHP variables begin with $. Statements normally end with a semicolon. Double-quoted strings can interpolate variables, and echo writes values into the response.

PHP tags and mixed templates

A .php file can switch between PHP and ordinary HTML. The short echo tag<?= ... ?> is a concise, always-available way to output a value inside markup.

profile.php
<?php

// A small server-rendered profile
$name = "Omar";
$goal = "Build secure web apps";
$lessonsCompleted = 1;
?>

<h1><?= htmlspecialchars($name) ?></h1>
<p>Goal: <?= htmlspecialchars($goal) ?></p>
<p>Lessons completed: <?= $lessonsCompleted ?></p>

Escape untrusted text

Values that may contain user input should be escaped for the place they are inserted. For HTML text, htmlspecialchars() prevents data from becoming executable markup.

PHP supports // and # line comments plus /* ... */ block comments. Comments should explain intent or constraints—not repeat obvious code.

Errors and a professional feedback loop

During development, keep error reporting visible, read the first useful message, note its file and line, and reduce the problem to the smallest failing expression. In production, errors belong in logs rather than public responses.

debug.php
<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

echo "Development errors are visible.";

Note

The official PHP manual is organized by language feature and function. When you meet an unfamiliar function, check its parameters, return value, examples, and version notes.

Practice challenge

Create about.php that stores your name, learning goal, and current module in variables, then renders them inside semantic HTML. Add one PHP comment explaining why you escape text.

Recap & quick check

Key takeaways

  • PHP executes on the server; clients receive its output, not its source code.
  • Use the CLI for scripts and php -S localhost:8000 for local web lessons.
  • PHP code starts with <?php; variables use $, statements usually end with semicolons, and echo produces output.
  • A PHP file can mix server-side logic with HTML, using <?= ... ?> for concise output.
  • Development errors should be visible locally and logged—but hidden from visitors—in production.

Quick check

1. Where does PHP normally execute in a web application?

2. Which command starts PHP's local development server on port 8000?

3. What is the safest default for displaying detailed errors in production?