What is PHP?
PHP (Hypertext Preprocessor) is a server-side scripting language designed for web development. PHP code runs on a web server and the result (HTML) is sent to the client's browser. It's embedded directly into HTML files.
🌐 Server-Side
PHP runs on the server before HTML is sent to the browser. The user never sees PHP code.
🗄️ Database Ready
Built-in support for MySQL, PostgreSQL, SQLite. Connecting to databases is simple and well-documented.
📦 Powers WordPress
WordPress, Drupal and Joomla are all built with PHP. Knowing PHP means being able to customize any of them.
Syntax & Variables
PHP code is written inside <?php ... ?> tags. Variables start with a $ sign. PHP is loosely typed — no need to declare variable types.
<?php // Variables start with $ $name = "Alice"; $age = 25; $price = 9.99; $active = true; // String concatenation with . echo "Hello, " . $name . "!"; // Or use double-quoted string interpolation echo "Hello, {$name}! You are {$age} years old."; // var_dump shows type and value var_dump($age); // int(25) var_dump($active); // bool(true) ?>
Control Flow
PHP uses the same control structures as C — if/elseif/else, for, while, foreach, and switch.
<?php $hour = 14; if ($hour < 12) { echo "Good morning!"; } elseif ($hour < 18) { echo "Good afternoon!"; } else { echo "Good evening!"; } // foreach loop (most common in PHP) $langs = ["PHP", "Python", "JS"]; foreach ($langs as $lang) { echo $lang . "\n"; } // for loop for ($i = 0; $i < 5; $i++) { echo $i; } ?>
Functions
Functions in PHP are defined with the function keyword. PHP 8+ added named arguments and union types for cleaner code.
<?php function greet(string $name, string $lang = "en"): string { return $lang === "ru" ? "Привет, {$name}!" : "Hello, {$name}!"; } echo greet("Bob"); // Hello, Bob! echo greet("Алиса", "ru"); // Привет, Алиса! // Arrow function (PHP 7.4+) $double = fn($x) => $x * 2; echo $double(7); // 14 ?>
Arrays
PHP arrays are powerful and versatile — they can act as lists, maps (associative arrays), or multidimensional structures.
<?php // Indexed array $colors = ["red", "green", "blue"]; echo $colors[0]; // red // Associative array (key => value) $user = [ "name" => "Alice", "age" => 30, "role" => "admin" ]; echo $user["name"]; // Alice // Useful array functions array_push($colors, "yellow"); sort($colors); $count = count($colors); // 4 $found = in_array("red", $colors); // true ?>
Forms & HTTP
PHP processes HTML form data via the superglobals $_GET, $_POST, and $_REQUEST. Always sanitize input to prevent security issues.
<!-- form.html --> <form action="process.php" method="POST"> <input type="text" name="username" required> <button type="submit">Submit</button> </form> <?php // process.php if ($_SERVER["REQUEST_METHOD"] === "POST") { $username = htmlspecialchars($_POST["username"]); echo "Welcome, {$username}!"; } ?>
PDO & MySQL
PDO (PHP Data Objects) is the modern way to connect PHP to a database. It supports prepared statements which prevent SQL injection attacks.
<?php $dsn = "mysql:host=localhost;dbname=myapp;charset=utf8"; $pdo = new PDO($dsn, "root", "password"); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Prepared statement — prevents SQL injection $stmt = $pdo->prepare("SELECT * FROM users WHERE email = ?"); $stmt->execute([$_POST["email"]]); $user = $stmt->fetch(PDO::FETCH_ASSOC); if ($user) { echo "Found: " . $user["name"]; } ?>
🛠 Mini Project: Contact Form
Build a complete contact form that validates input on the server side and stores messages in a text file (no database needed).
<?php $errors = []; $success = false; if ($_SERVER["REQUEST_METHOD"] === "POST") { $name = trim($_POST["name"] ?? ""); $email = trim($_POST["email"] ?? ""); $message = trim($_POST["msg"] ?? ""); if (!$name) $errors[] = "Name required"; if (!filter_var($email, FILTER_VALIDATE_EMAIL)) $errors[] = "Valid email required"; if (strlen($message) < 10) $errors[] = "Message too short"; if (empty($errors)) { $line = date("Y-m-d H:i") . " | {$name} | {$email} | {$message}\n"; file_put_contents("messages.txt", $line, FILE_APPEND); $success = true; } } ?>