📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials PHP for Beginners PHP 8 New Features

PHP 8 New Features

5 min read Quiz at the end
PHP 8.0 introduced the JIT compiler for better performance, named arguments, union types, the match expression, the null safe operator, and constructor property promotion. It made type error handling much more consistent.

PHP 8 Highlights

// 1. Named arguments
htmlspecialchars(string: $s, double_encode: false);

// 2. Union types
function process(int|string $input): int|false {}

// 3. Match expression
$val = match($x) { 1 => "one", 2 => "two", default => "other" };

// 4. Null safe operator
$city = $user?->address?->city;

// 5. Constructor promotion
class Point { public function __construct(
    public float $x, public float $y
) {} }

// 6. str_contains / str_starts_with / str_ends_with
str_contains("Hello World", "World");    // true
str_starts_with("Hello", "He");         // true
str_ends_with("Hello", "lo");           // true

// 7. Attributes (annotations)
#[Route("/users", methods: ["GET"])]
class UserController {}

// 8. throw as expression
$name = $input ?? throw new InvalidArgumentException("Required");

// 9. JIT compiler (Just-In-Time) — faster numeric code
Topic Quiz · 5 questions

Test your understanding before moving on

1. Which PHP 8 feature allows checking multiple conditions in one arm?
💡 match($x) { 1, 2 => "one or two" } uses multiple values per arm.
2. str_contains() was added in PHP:
💡 str_contains(), str_starts_with(), str_ends_with() were all added in PHP 8.0.
3. What is the null safe operator?
💡 ?-> calls a method/property only if the left side is not null.
4. PHP 8 JIT compiler helps most with:
💡 JIT (Just-In-Time) compilation gives the biggest speed gains for CPU-intensive operations.
5. Named arguments were added in PHP:
💡 Named arguments are a PHP 8.0 feature.