What is New in PHP 8

PHP 8 is a big update with many new features and improvements. It helps developers write better and faster code. In this post, I will explain the most useful changes in PHP 8 for daily programming.

1. JIT Compiler

PHP 8 has a JIT compiler. JIT means Just in Time. It can make code run faster by compiling parts of code during runtime. It is very helpful for tasks like image processing or heavy math.

2. Union Types

Before PHP 8, you could only have one type for function arguments or return. Now you can have more than one:

function test(int|string $value) {
    // works with int or string
}

3. Named Arguments

You can now pass arguments by name. This means you can skip optional ones and make the code easier to read:

function createUser($name, $age, $country) {}

createUser(name: 'Ali', country: 'Pakistan', age: 30);

4. Match Expression

Match is like switch but more powerful and clean:

$result = match($status) {
    1 => 'Active',
    2 => 'Inactive',
    3 => 'Pending',
    default => 'Unknown',
};

5. Nullsafe Operator

It helps avoid errors if a value is null. Instead of checking every time:

$username = $user?->profile?->username;

This will return null if any part is null.

6. Attributes

You can now use native attributes (annotations) in classes. It is useful for defining rules or metadata directly:

#[Route("/home")]
class HomeController {}

7. Constructor Property Promotion

It makes class properties shorter and cleaner:

class User {
    public function __construct(
        public string $name,
        public int $age,
    ) {}
}

Final Thoughts

PHP 8 brings many good changes. These features help you write modern, safe, and clear code. You do not need to learn everything in one day. Try one feature at a time in your next project.

0
Subscribe to my newsletter

Read articles from Rameez Karamat Bhatti directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rameez Karamat Bhatti
Rameez Karamat Bhatti

I am a full-stack developer currently working with PHP, Laravel, Tailwind CSS, and Livewire. I also work extensively with Inertia.js to build seamless single-page applications using Vue.js and Laravel. I have experience building reactive frontends with Vue.js and React, and robust backends with REST APIs. I am highly proficient in MySQL, MongoDB, and also comfortable working with PostgreSQL for relational data projects. My focus is on clean, scalable code and building user-focused digital products.