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.
Why PHP?
Despite jokes about its inconsistencies, PHP powers a remarkable portion of the web:
- ~77% of websites with a known server-side language use PHP
- WordPress (43% of all websites) is built on PHP
- Wikipedia, Facebook (originally), and countless others
- Mature ecosystem: Composer packages, Laravel/Symfony frameworks
- Easy deployment: Just upload
.phpfiles
For learning server-side concepts, PHP is excellent because:
- The connection between URL and file is clear (
/cart.php→cart.php) - Superglobals make request data obvious (
$_GET,$_POST) - Sessions work out of the box (
session_start()) - No build step - just save and refresh
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']);
PHP in the Ecosystem
While this course focuses on PHP fundamentals, modern PHP development often involves:
- Composer: Package manager (like npm for Node.js)
- Frameworks: Laravel, Symfony, Slim for structured applications
- ORMs: Eloquent, Doctrine for database abstraction
- Testing: PHPUnit for automated tests
- PHP 8+: Modern features like named arguments, attributes, JIT
The fundamentals you learn here apply regardless of framework choice.
Back to Home | Execution Models | State Management | PHP Tutorial