Learn PHP

Pragmatic server-side language that powers a large share of the web

25 lessons · 6 hours · 4 levels · covers PHP 8.4

PHP is a dynamically typed language built for the web: every request runs a script that reads input and produces a response. Modern PHP has strict types, enums, readonly classes, attributes and a JIT, and an ecosystem built around Composer, Laravel and Symfony. It runs WordPress, Wikipedia and countless business applications, so it is one of the most employable back-end skills.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics8 lessons · 2 hours in total
2. PracticalUse it productively at work, with the usual idioms16 lessons · 3 hours in total
3. AdvancedThe less common features, performance and trade-offs21 lessons · 4 hours in total
4. ExpertInternals and the edge cases experts know25 lessons · 6 hours in total

The PHP tree

PHPRunning PHPRunning PHPVariables and typesVariables and typesControl flow and matchControl flow andmatchStringsStringsArraysArraysFunctions and argumentsFunctions andargumentsClasses and objectsClasses and objectsHandling HTTP requestsHandling HTTPrequestsArray functions and sortingArray functions andsortingClosures and callablesClosures andcallablesStrict types and the type systemStrict types and thetype systemErrors and exceptionsErrors andexceptionsInterfaces, abstract classes and traitsInterfaces, abstractclasses and traitsEnums and readonly objectsEnums and readonlyobjectsNamespaces, Composer and autoloadingNamespaces,Composer and auto…Databases with PDODatabases with PDO
Essentials and Practical shown (16 of 25 lessons); the full list is below. Click a lesson to read it.

All 25 lessons, in order

Language basics

  1. Running PHP · Essentials · 8 min

    Write a PHP script, run it from the command line and see how the <?php tag and echo work.

  2. Variables and types · Essentials · 10 min

    Use $variables and PHP's scalar types: int, float, string, bool and null, plus how comparisons juggle types.

  3. Control flow and match · Essentials · 10 min

    Branch with if, elseif and match, and repeat with for, while and foreach.

Strings and arrays

  1. Strings · Essentials · 10 min

    Quote strings, interpolate variables, build multi-line text with heredoc, and use the core string functions.

  2. Arrays · Essentials · 12 min

    Use PHP's one array type as a list, a dictionary or both, and loop over it with foreach.

  3. Array functions and sorting · Practical · 14 min

    Transform arrays with array_map, array_filter and array_reduce, and sort them with the right sort function.

Functions and types

  1. Functions and arguments · Essentials · 12 min

    Declare functions with typed parameters, defaults, named arguments and variadics.

  2. Closures and callables · Practical · 12 min

    Write anonymous functions, arrow functions and first-class callables, and control what they capture.

  3. Strict types and the type system · Practical · 14 min

    Turn on strict_types and use union, nullable, mixed, never and other type declarations.

  4. Errors and exceptions · Practical · 12 min

    Throw and catch exceptions, understand the Throwable hierarchy and write custom exception classes.

  5. Generators and iterators · Advanced · 14 min

    Produce values lazily with yield, delegate with yield from, and process large data in constant memory.

Object-oriented PHP

  1. Classes and objects · Essentials · 14 min

    Define classes with properties, methods and constructor promotion, and create objects with new.

  2. Interfaces, abstract classes and traits · Practical · 14 min

    Define contracts with interfaces, share base behaviour with abstract classes and reuse code with traits.

  3. Enums and readonly objects · Practical · 12 min

    Model fixed sets of values with enums and build immutable value objects with readonly properties.

  4. Property hooks and asymmetric visibility · Advanced · 14 min

    Use PHP 8.4 property hooks for computed and validated properties, and private(set) for read-only-from-outside state.

PHP on the web

  1. Handling HTTP requests · Essentials · 14 min

    Read query strings and form posts from superglobals, send headers, and escape output safely.

  2. Databases with PDO · Practical · 15 min

    Connect to a database with PDO, run prepared statements, fetch rows and use transactions.

  3. Sessions, passwords and web security · Advanced · 16 min

    Keep users logged in with sessions, hash passwords correctly and defend against CSRF, XSS and session fixation.

Tooling and ecosystem

  1. Namespaces, Composer and autoloading · Practical · 14 min

    Organise code into namespaces, install packages with Composer and autoload classes with PSR-4.

  2. Testing with PHPUnit · Advanced · 14 min

    Write unit tests with PHPUnit: test classes, assertions, data providers and expected exceptions.

  3. Attributes and static analysis · Advanced · 14 min

    Read and define attributes, and catch bugs before runtime with PHPStan generics and strict levels.

Runtime and internals

  1. Request lifecycle, SAPIs and PHP-FPM · Expert · 16 min

    Understand how a request reaches PHP through a SAPI, PHP-FPM's process model and long-running worker modes.

  2. Values, references and memory · Expert · 16 min

    See how PHP stores values: copy-on-write, reference counting, PHP references, and the cycle collector.

  3. OPcache, preloading and the JIT · Expert · 14 min

    Configure OPcache for production, use preloading, and know when the JIT helps and when it doesn't.

  4. Fibers and async PHP · Expert · 15 min

    Understand Fibers, PHP's primitive for pausable functions, and how async libraries build event loops on them.