Introduction to PHP
PHP (Hypertext Preprocessor) is an open-source server-side scripting language built for the web. It runs on the server, generates HTML, and integrates natively with MySQL, PostgreSQL, Redis, and every major hosting provider. Over 75% of websites with a known server-side language still use PHP — including WordPress, Wikipedia, and Facebook’s early stack.
What is PHP?
PHP scripts execute on the server before the response reaches the browser. The web server (Apache, Nginx, or PHP’s built-in server) passes requests to the PHP interpreter, which runs your code and returns HTML, JSON, or other output.
Key characteristics:
- Embedded in HTML — switch between markup and logic with
<?php ... ?> - Loosely typed — variables do not require explicit type declarations (strict types available since PHP 7)
- Request-oriented — each HTTP request bootstraps a fresh execution context
- Mature ecosystem — Composer for packages, Laravel and Symfony for applications
History and Evolution
| Year | Milestone |
|---|---|
| 1994 | Rasmus Lerdorf creates Personal Home Page Tools |
| 1997 | PHP/FI 2.0 — form handling, guestbook |
| 1998 | PHP 3.0 — rewritten engine, wide adoption |
| 2000 | PHP 4.0 — Zend Engine, sessions, output buffering |
| 2004 | PHP 5.0 — OOP, exceptions, PDO for databases |
| 2015 | PHP 7.0 — 2× performance, scalar type hints, ?? operator |
| 2020 | PHP 8.0 — JIT compiler, named arguments, union types, attributes |
| 2023 | PHP 8.2+ — readonly classes, enums, improved performance |
PHP 8.x is the only supported line for new projects. PHP 7 reached end of life in 2022.
How PHP Fits in a Web Request
Browser → Nginx/Apache → PHP-FPM → Your Script → Database
↓ ↓
Static files HTML / JSON response
A minimal PHP page:
<?php
declare(strict_types=1);
$name = $_GET['name'] ?? 'World';
?>
<!DOCTYPE html>
<html>
<head><title>Greeting</title></head>
<body>
<h1><?= htmlspecialchars($name, ENT_QUOTES, 'UTF-8') ?></h1>
</body>
</html>
Run locally:
php -S localhost:8000
# Visit http://localhost:8000/?name=Simon
Modern PHP Example
<?php
declare(strict_types=1);
enum Status: string {
case Active = 'active';
case Inactive = 'inactive';
}
final class User
{
public function __construct(
public readonly int $id,
public string $name,
public Status $status,
) {}
}
function findActiveUsers(array $users): array
{
return array_filter(
$users,
fn(User $u) => $u->status === Status::Active
);
}
$users = [
new User(1, 'Alice', Status::Active),
new User(2, 'Bob', Status::Inactive),
];
foreach (findActiveUsers($users) as $user) {
echo $user->name . PHP_EOL;
}
// Alice
PHP vs Other Languages
PHP vs Python
Both are beginner-friendly. PHP was designed specifically for web requests and ships with session handling, cookie support, and superglobals ($_GET, $_POST). Python is a general-purpose language; web development uses Django, Flask, or FastAPI. Choose PHP when deploying to shared hosting or maintaining WordPress/Laravel codebases.
PHP vs JavaScript (Node.js)
JavaScript runs in browsers natively and on servers via Node.js. PHP runs only on the server but has decades of web-specific tooling (Apache modules, PHP-FPM, hosting panels). Many teams use PHP for server-rendered pages and JavaScript for interactive front ends.
PHP vs Java
Java targets large enterprise systems with strict typing and long-running JVM processes. PHP bootstraps per request — simpler deployment on shared hosting, lower memory per idle connection. Laravel and Symfony bring enterprise patterns (DI, ORM, queues) to PHP.
Major Frameworks and Tools
| Tool | Purpose |
|---|---|
| Composer | Dependency manager (like npm for PHP) |
| Laravel | Full-stack MVC framework with Eloquent ORM |
| Symfony | Component-based framework for enterprise apps |
| PHPUnit | Unit and integration testing |
| PHPStan / Psalm | Static analysis for type safety |
Common Use Cases
- Content management — WordPress, Drupal, Joomla
- E-commerce — Magento, WooCommerce, custom Laravel shops
- REST APIs — Laravel, Slim, Symfony API Platform
- Legacy modernization — gradual PHP 8 upgrades with typed properties and enums
Security Essentials
PHP’s superglobals contain untrusted user input. Always:
- Escape output:
htmlspecialchars()for HTML, prepared statements for SQL - Validate input on the server, not just in JavaScript
- Use
declare(strict_types=1)and type hints in new code - Keep PHP and extensions updated
Getting Started
- Install PHP 8.2+ and Composer — see Installing PHP
- Choose an editor — see PHP IDEs
- Write your first script — Basic PHP Syntax
What Comes Next
This track covers syntax, forms, sessions, databases, OOP, Composer, Laravel, Symfony, security, performance, and deployment. PHP remains one of the fastest paths from zero to a deployed web application — and modern PHP is a far cry from the language’s early reputation.