PHP Reference ยท Concepts and Reference
PHP 8 feature tour
A guided tour of the biggest PHP 8 features: union types, match, named arguments, nullsafe, enums, readonly, fibers, first-class callables and more.
PHP 8.0 was the biggest release in a decade, and every release since (8.1 to 8.5) has kept adding to the language. This page walks through the features that change how you write everyday code. Each item names the version that introduced it, so you can tell what your server supports.
You do not need to learn them all at once. Start with the ones that remove boilerplate (constructor promotion, match, the nullsafe operator), then adopt the type-system features (union types, enums, readonly) as you touch more code.
<?php $city = $user?->address?->city ?? "unknown"; // 8.0 nullsafe $label = match ($status) { 1 => "on", 0 => "off" }; // 8.0 match
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
PHP 8.0
- Union types (
int|string) and themixedandstaticreturn types. - Named arguments: pass arguments by parameter name, skip defaults, in any order.
- Constructor property promotion: declare and assign properties in the constructor signature.
matchexpression: strict comparison, no fall-through, throwsUnhandledMatchErrorwhen nothing matches.- Nullsafe operator
?->: short-circuits a whole chain when a link isnull. - Attributes (
#[Route("/")]): structured metadata, readable through Reflection. See attributes. str_contains(),str_starts_with(),str_ends_with(),get_debug_type(), andthrowas an expression.- Saner behavior: string-to-number comparison follows new rules (loose comparison tables), and many warnings became
TypeErrororValueErrorexceptions.
PHP 8.1
- Enums, pure and backed, with methods and interfaces.
readonlyproperties: write once, then immutable.- First-class callable syntax:
strlen(...),$obj->method(...). - Fibers: see Fibers.
neverreturn type, pure intersection types (A&B),newin initializers,array_is_list(), and the0o17octal prefix.
PHP 8.2 to 8.5
- 8.2:
readonlyclasses,null,falseandtrueas standalone types, DNF types such as(A&B)|null, constants in traits, and the#[SensitiveParameter]attribute. Dynamic properties were deprecated. - 8.3: typed class constants,
#[Override],json_validate(), dynamic class-constant fetch (Foo::{$name}). - 8.4: property hooks and asymmetric visibility (
public private(set)),newwithout extra parentheses (new Foo()->bar()), lazy objects,array_find(),array_any()andarray_all(). - 8.5: the pipe operator
|>, the#[NoDiscard]attribute,array_first()andarray_last(), and a built-in URI extension.
Check what your own install supports by printing PHP_VERSION and PHP_VERSION_ID.
Usage notes
- Adopt features gradually. Code that runs on 8.0 runs on later 8.x with few changes, but read the deprecated features page before upgrading.
- Tools such as Rector and PHP_CodeSniffer can rewrite older code to use promotion,
matchand typed properties automatically. composer.jsoncan pin the minimum version ("php": "^8.2"), which stops Composer from installing your package on older runtimes.