PHP Cheat Sheet

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

A single-page, colour-coded summary of the PHP language, written against PHP 8.5 (8.5.11), with additions from 8.0 onwards labelled by version and upcoming 8.6 items marked (versions verified 2026-10-05) — PHP tags and CLI one-liners, the type list and declarations (union, intersection and DNF types, nullable, never, strict_types), the PHP 8 comparison and juggling rules, operators (??, ??=, <=>, ?->, |>, ...), control flow including match, string and array one-liners (including array_find() and array_first()), functions (named arguments, variadics, by-reference parameters), closures, arrow functions and first-class callables, a class skeleton showing constructor promotion, readonly, asymmetric visibility and property hooks, interfaces, abstract classes, traits and enums, magic methods, the predefined attributes, the exception hierarchy with try/catch/finally, generators and fibers, namespaces and use, date/time, JSON and file one-liners, password_hash() and sodium, Composer commands and the composer.json autoload block, the PSR list, PHPUnit and Pest snippets, and a strip of what changed since older tutorials — handy for quick memorization or as a downloadable PDF desk reference.

Download the PHP Cheat Sheet (PDF)

For the full explanation, every option, and additional examples, see the dedicated pages:

Start here — PHP Reference.

Worked example

The example combines a backed enum, match, array_map() and an arrow function.

<?php

declare(strict_types=1);

enum Status: string
{
    case Open = 'open';
    case Closed = 'closed';
}

function label(Status $status): string
{
    return match ($status) {
        Status::Open => 'Open for loans',
        Status::Closed => 'Closed',
    };
}

$titles = ['Solaris', 'Kindred', 'Dawn'];
$lengths = array_map(fn (string $title): int => strlen($title), $titles);

echo label(Status::from('open')), "\n";
echo implode(', ', $lengths), "\n";
Open for loans
7, 7, 4

Status::from() turns the string into an enum case, match returns a value and throws if no arm fits, and the arrow function captures nothing from the outer scope.