PHP Overview

Server-Side Scripting for the Web

"PHP's defining characteristic: it's designed to be embedded directly in HTML. This makes it a templating language as much as a programming language."

CSE 135 — Full Overview | Review Questions

Section 1What is PHP?

A server-side language designed to be embedded in HTML.

History & The Embedded Model

Created by Rasmus Lerdorf in 1995 as "Personal Home Page" tools. PHP (PHP: Hypertext Preprocessor) is designed to be embedded directly in HTML.

<!DOCTYPE html> <html> <body> <h1>Welcome</h1> <p>The time is: <?php echo date('H:i:s'); ?></p> <p>You are visitor #<?php echo $visitorCount; ?></p> </body> </html>
The server processes <?php ?> blocks, replacing them with output. The browser receives pure HTML — it never sees PHP code. This makes PHP a templating language as much as a programming language.

Section 2How PHP Executes

The mod_php request flow and the per-request model.

mod_php Request Flow

Request Flow with mod_php: ┌─────────┐ ┌───────────────────────────────────────┐ │ Browser │ │ Apache + mod_php │ │ │ GET /index.php │ ┌─────────────────────────────────┐ │ │ │ ──────────────────▶│ │ PHP Interpreter │ │ │ │ │ │ │ │ │ │ │ │ 1. Read index.php │ │ │ │ │ │ 2. Parse PHP code │ │ │ │ │ │ 3. Execute (run logic) │ │ │ │ │ │ 4. Generate HTML output │ │ │ │ HTML response │ │ │ │ │ │ ◀──────────────────│ └─────────────────────────────────┘ │ └─────────┘ └───────────────────────────────────────┘

PHP runs inside Apache as a module. Apache receives the request, hands the .php file to the PHP interpreter, and returns the generated HTML.

Per-Request Execution

Each PHP request starts fresh. Variables don't persist between requests. When the response is sent, everything is destroyed. That's what sessions and databases are for.

PHP (per-request)

  • Process starts on each request
  • Variables created
  • Logic executes
  • HTML generated
  • Process ends — everything gone

Node.js (persistent)

  • Process starts once
  • Variables persist in memory
  • app.listen(3000)
  • Handles many requests
  • Runs until you stop it
This is the fundamental architectural difference. PHP runs inside a web server and processes one request at a time. Node.js IS the server and runs continuously.

Section 3Why PHP?

Market dominance and learning advantages.

Market Share & Learning Benefits

Market Dominance

  • ~77% of websites with a known server-side language use PHP
  • WordPress (43% of all websites) is built on PHP
  • Wikipedia, Facebook (originally) use PHP
  • Mature ecosystem: Composer, Laravel, Symfony

Why Learn PHP?

  • URL-to-file mapping: /cart.phpcart.php
  • Superglobals: $_GET, $_POST make request data obvious
  • Built-in sessions: session_start() just works
  • No build step: Save and refresh
Easy deployment: just upload .php files. No npm install, no process managers, no build steps. This makes the connection between code and result immediate and clear.

Section 4PHP Syntax Essentials

Variables, control structures, superglobals, and common operations.

Variables & Control Structures

Variables & Output

<?php // Variables start with $ $name = "Alice"; $count = 42; $prices = [9.99, 14.99, 29.99]; // Output echo "Hello, $name!"; echo "Count: " . $count; ?>

Control Structures

<?php if ($age >= 18) { echo "Adult"; } elseif ($age >= 13) { echo "Teen"; } else { echo "Child"; } foreach ($items as $item) { echo "<li>$item</li>"; } ?>

Variables start with $, strings use . for concatenation, and double-quoted strings interpolate variables directly.

Superglobals & Quick Reference

SuperglobalContainsExample
$_GETURL query parameters$_GET['id'] from ?id=42
$_POSTForm data (POST method)$_POST['username']
$_SESSIONSession data$_SESSION['user_id']
$_COOKIECookie values$_COOKIE['theme']
$_SERVERServer/request info$_SERVER['REQUEST_METHOD']
// Read form input safely $username = htmlspecialchars($_POST['username'] ?? '', ENT_QUOTES, 'UTF-8'); // Start a session session_start(); $_SESSION['user_id'] = 42; // Set a cookie (1 hour) setcookie('theme', 'dark', time() + 3600, '/'); // Return JSON header('Content-Type: application/json'); echo json_encode(['status' => 'ok']);

Section 5PHP in the Ecosystem

Composer, frameworks, ORMs, testing, and PHP 8+.

Modern PHP Development

  • Composer: Package manager (like npm for Node.js) — manages dependencies via composer.json
  • Frameworks: Laravel, Symfony, Slim for structured, MVC-based applications
  • ORMs: Eloquent (Laravel), Doctrine (Symfony) for database abstraction
  • Testing: PHPUnit for automated unit and integration tests
  • PHP 8+: Named arguments, attributes, union types, fibers, JIT compilation
The fundamentals apply regardless of framework choice. Understanding how PHP processes requests, reads superglobals, and generates output is the foundation that every framework builds upon.

SummaryKey Takeaways

5 concepts of PHP server-side development in one table.

PHP Overview at a Glance

ConceptKey Takeaway
What is PHP?A server-side language embedded in HTML. The server processes <?php ?> blocks and the browser receives pure HTML.
Execution ModelPHP runs inside Apache (mod_php). Each request starts fresh — variables don't persist. Sessions and databases handle state.
Why PHP?~77% market share, WordPress, simple deployment. URL-to-file mapping and superglobals make concepts transparent.
Syntax Essentials$ variables, superglobals ($_GET, $_POST, $_SESSION), and built-in functions for sessions, cookies, and JSON.
EcosystemComposer, Laravel/Symfony, Eloquent/Doctrine, PHPUnit, PHP 8+. Fundamentals apply regardless of framework.