PHP Overview

What is PHP?

PHP (PHP: Hypertext Preprocessor) is a server-side scripting language designed specifically for web development. Created by Rasmus Lerdorf in 1995, it was originally a set of CGI scripts called "Personal Home Page" tools.

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.

<!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 the <?php ... ?> blocks, replacing them with their output. The browser receives pure HTML.

How PHP Executes

PHP typically runs as an Apache module (mod_php) or via PHP-FPM (FastCGI Process Manager). Either way, the web server hands off .php files to the PHP interpreter.

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 │ │ │ │ │ │ ◀──────────────────│ └─────────────────────────────────┘ │ └─────────┘ └───────────────────────────────────────┘
Key point: Each PHP request starts fresh. Variables don't persist between requests (that's what sessions and databases are for).

Why PHP?

Despite jokes about its inconsistencies, PHP powers a remarkable portion of the web:

For learning server-side concepts, PHP is excellent because:

PHP Syntax Essentials

Variables and Output

<?php
// Variables start with $
$name = "Alice";
$count = 42;
$prices = [9.99, 14.99, 29.99];  // Array

// Output
echo "Hello, $name!";           // Prints: Hello, Alice!
echo "Count: " . $count;        // Concatenation with .
?>

Control Structures

<?php
// Conditionals
if ($age >= 18) {
    echo "Adult";
} elseif ($age >= 13) {
    echo "Teen";
} else {
    echo "Child";
}

// Loops
foreach ($items as $item) {
    echo "<li>$item</li>";
}
?>

Superglobals

PHP provides special arrays that are always available:

Superglobal Contains Example
$_GET URL query parameters $_GET['id'] from ?id=42
$_POST Form data (POST method) $_POST['username']
$_SESSION Session data $_SESSION['user_id']
$_COOKIE Cookie values $_COOKIE['theme']
$_SERVER Server/request info $_SERVER['REQUEST_METHOD']

Quick Reference: Common Operations

// Read form input safely
$username = $_POST['username'] ?? '';
$username = htmlspecialchars($username, ENT_QUOTES, 'UTF-8');

// Check request method
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Handle form submission
}

// Start a session
session_start();
$_SESSION['user_id'] = 42;

// Set a cookie (1 hour expiry)
setcookie('theme', 'dark', time() + 3600, '/');

// Redirect
header('Location: /dashboard.php');
exit;

// Return JSON
header('Content-Type: application/json');
echo json_encode(['status' => 'ok']);

Ready to Learn PHP?

Work through our hands-on tutorial with live demos.

Start the PHP Tutorial →

PHP in the Ecosystem

While this course focuses on PHP fundamentals, modern PHP development often involves:

The fundamentals you learn here apply regardless of framework choice.


Back to Home | Execution Models | State Management | PHP Tutorial