PHP Cheat Sheet
PHP syntax, associative arrays, superglobals, and object-oriented features for server-side web development and dynamic scripting.
2 PagesBeginnerMar 22, 2026
Basic Syntax
Variables, control flow, and output.
php
<?php$age = 30;$name = "Ada";$pi = 3.14159;if ($age >= 18) { echo "$name is an adult\n";}for ($i = 0; $i < 5; $i++) { echo "Count: $i\n";}?>
Arrays
Indexed and associative arrays.
php
<?php$nums = [5, 3, 1, 4, 2];sort($nums); // [1, 2, 3, 4, 5]$ages = ["Alice" => 30, "Bob" => 25]; // associative array$ages["Carol"] = 28;echo $ages["Alice"]; // 30$doubled = array_map(fn($n) => $n * 2, $nums);$evens = array_filter($nums, fn($n) => $n % 2 === 0);?>
Object-Oriented PHP
Classes, constructors, and visibility.
php
<?phpclass Person { public string $name; private int $age; public function __construct(string $name, int $age) { $this->name = $name; $this->age = $age; } public function greet(): string { return "Hello, {$this->name}"; }}$p = new Person("Ada", 30);echo $p->greet();?>
Superglobals
Built-in arrays always available in scope.
- $_GET- associative array of URL query string parameters
- $_POST- associative array of form data sent via POST
- $_SESSION- persists data across requests for a logged-in user
- $_SERVER- server and execution environment information (e.g. REQUEST_METHOD)
- $_ENV- environment variables available to the script
- $_FILES- info about files uploaded via HTTP POST
String Functions
Commonly used string helpers.
php
$s = "Hello World";strlen($s); // 11strtoupper($s); // "HELLO WORLD"str_replace("World", "PHP", $s); // "Hello PHP"substr($s, 0, 5); // "Hello"explode(" ", $s); // ["Hello", "World"]implode("-", ["a", "b"]); // "a-b"trim(" x "); // "x"str_contains($s, "World"); // true (PHP 8+)sprintf("%05.2f", 3.1); // "03.10"
PDO Database Access
Prepared statements to prevent SQL injection.
php
$pdo = new PDO("mysql:host=localhost;dbname=app", $user, $pass, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,]);$stmt = $pdo->prepare("SELECT * FROM users WHERE age > :age");$stmt->execute(["age" => 18]);$rows = $stmt->fetchAll();$insert = $pdo->prepare("INSERT INTO users (name) VALUES (?)");$insert->execute(["Ada"]);echo $pdo->lastInsertId();
Modern PHP Features
Syntax from PHP 7.4 through 8.x.
php
// Arrow functions (7.4)$doubled = array_map(fn($x) => $x * 2, [1, 2, 3]);// Named arguments + nullsafe (8.0)$name = $user?->getProfile()?->name;// Match expression (8.0)$label = match($code) { 200, 201 => "OK", 404 => "Not Found", default => "Unknown",};// Constructor promotion (8.0)class Point { public function __construct( public float $x = 0, public float $y = 0, ) {}}
Array Functions
Frequently used array utilities.
- array_map- apply a callback to every element
- array_filter- keep elements passing a callback test
- array_reduce- fold an array into a single value
- array_merge- combine arrays, re-indexing numeric keys
- in_array- check whether a value exists (use strict flag)
- array_keys / array_values- extract keys or values
- array_column- pluck one column from an array of rows
- usort- sort in place with a comparison callback
Pro Tip
Always use prepared statements (PDO with bound parameters) instead of concatenating user input into SQL strings, to prevent SQL injection.
Was this cheat sheet helpful?
Explore Topics
#PHP#PHPCheatSheet#Programming#Beginner#BasicSyntax#Arrays#ObjectOrientedPHP#Superglobals#OOP#DataStructures#WebDevelopment#CommandLine#CheatSheet#SkillVeris