Skip to content
LumenaryAcademy

Lumenary CS · Programming languages

PHP

The language that grew up with the web, as it is written today: typed, object-oriented and fast, from a first script to safe, tested, professional applications.

PHP is a general-purpose scripting language best known for building websites and web applications. It first appeared in 1995 as a small set of tools for personal home pages and grew into the language behind a large share of the web: WordPress, MediaWiki (the software that runs Wikipedia), online shops, and countless applications built with the Laravel and Symfony frameworks. Modern PHP (version 8 and later) is a very different language from the one its old reputation describes: it has strict type checking on request, enums, readonly properties, a just-in-time compiler, and a mature ecosystem of packages, standards and tools.

This course is for anyone who already knows what a variable, a loop and a function are: the Start level of Lumenary CS or equivalent experience in any language. It begins with PHP's history and your first scripts, works through the core language, arrays and strings, classes, interfaces, traits and enums, and errors and exceptions, and ends with handling web requests safely (validation, escaping and prepared statements), Composer and the PSR standards, testing, the major frameworks, and a complete small project.

To run the code, install PHP from php.net/downloads or your operating system's package manager (on macOS, Homebrew; on Windows, the builds at windows.php.net); the official Docker image php:cli also works. There is no official online playground, but the community site 3v4l.org runs a snippet on many PHP versions at once. Every complete program in these lessons was run with PHP 8.6 by Lumenary's test suite, and the output shown is the output it printed. The lessons were drafted with AI assistance and are awaiting review by a human computer scientist.

AI-draftedAwaiting review by a computer scientist
  • Chloe · The Designer
    “Why is the language built this way?”
  • Margaret · The Profiler
    “What does it print, and what does it cost?”
  • Gideon · The Language Lawyer
    “What exactly does the language guarantee?”

Foundations · 4 lessons

Foundations

Install PHP and run scripts from the command line, then learn the core language: values, types and strict mode, control flow and match, functions, and PHP's all-purpose arrays and its strings.

  1. 1Getting started with PHPWhere PHP came from, how a PHP program runs on the web and on the command line, and your first scripts. 45 min
  2. 2Values, types and control flowPHP's types, how it converts between them, why strict_types belongs at the top of every file, and the statements that make decisions and repeat work. 50 min
  3. 3FunctionsTyped parameters and return values, named and variadic arguments, closures and arrow functions, and the newer ways PHP composes functions. 55 min
  4. 4Arrays and stringsPHP's one data structure that does everything, the standard functions that work on it, and strings as the bytes they really are. 60 min

Practice · 3 lessons

Practice

Model programs with classes, interfaces and traits, make illegal states unrepresentable with enums and readonly data, and handle failure with exceptions.

  1. 5Classes, interfaces and traitsObjects with typed properties and promoted constructors, contracts with interfaces, shared code with abstract classes and traits, and PHP 8.4's property hooks. 60 min
  2. 6Enums, readonly data and the type systemMake invalid states impossible to represent with enums and immutable value objects, and use the whole of PHP's type system, including what it leaves to static analysis. 55 min
  3. 7Errors and exceptionsPHP's two ways of reporting trouble, the Throwable hierarchy, try, catch and finally, your own exception types, and how professional applications handle failure. 50 min

Professional · 2 lessons

Professional

Handle web requests and forms safely, work the way PHP teams do with Composer, the PSR standards, PHPUnit and static analysis, meet Laravel and Symfony, and build a complete project.

  1. 8Handling web requests and forms safelyHow a request reaches your PHP code, and the habits that keep a site safe from its own input — validation, escaping, prepared statements with PDO, passwords and CSRF tokens. 60 min
  2. 9The ecosystem, and a complete projectHow PHP teams work every day — Composer, autoloading and the PSR standards, PHPUnit, static analysis and formatting, Laravel and Symfony — and a small, complete application built the same way. 70 min

Further reading and resources

  • php.netThe official home of PHP: downloads, release announcements and news.
  • The PHP ManualThe official documentation: the language reference and every function and class, with user notes.
  • PHP language specificationA written specification of the language, started in 2014. It lags behind the newest releases; the manual and the RFCs are the current reference.
  • PHP RFCsEvery change to the language is proposed, discussed and voted on as an RFC. The authoritative record of what each version added and why.
  • Supported versionsWhich PHP versions still receive bug and security fixes, and until when.
  • ComposerPHP's dependency manager and autoloader generator, used by virtually every modern PHP project.
  • 3v4l.orgA community-run online sandbox that runs a snippet on many PHP versions side by side.