Catching Up with PHP 8 and Beyond

If you have been using PHP 5 or PHP 6 and missed the updates in PHP 7 and 8, do not worry. A lot has changed, and many new features were added to make coding easier, faster, and more secure. In this post, I will explain the most important changes in simple language.


PHP 7 Highlights

1. Scalar Type Declarations

PHP 7 lets you define the type of data that functions expect. For example:

function add(int $a, int $b): int {
    return $a + $b;
}

This makes your code safer because it only accepts integers.

2. Return Type Declarations

You can now tell what type a function should return:

function getName(): string {
    return "Ali";
}

3. Null Coalescing Operator

This is a short way to check if a variable is set:

$name = $_GET['name'] ?? 'Guest';

It means if ‘name’ is not set, use ‘Guest’.

4. Spaceship Operator

This is useful for comparing two values:

$result = $a <=> $b;

It returns -1 if a is less than b, 0 if equal, 1 if greater.

5. Anonymous Classes

You can now create classes without a name:

$obj = new class {
    public function sayHello() {
        return "Hello";
    }
};

6. Throwable Errors

PHP 7 introduced a new way to handle errors using Throwable so you can catch all types of exceptions.


PHP 8 Features

1. JIT Compiler

JIT stands for Just In Time compiler. It can improve performance for some tasks like math or processing data. It is not always faster for web apps but helps in heavy backend work.

2. Union Types

Functions can now accept more than one type:

function test(int|string $value) {}

This means you can pass either an integer or a string.

3. Named Arguments

You can pass arguments by name. This helps skip optional values and is easier to read:

function register($name, $email, $age) {}

register(name: 'Ali', age: 25, email: 'ali@example.com');

4. Match Expression

This is a better version of switch:

$role = match($id) {
    1 => 'Admin',
    2 => 'User',
    3 => 'Guest',
    default => 'Unknown',
};

It is cleaner and does not require break.

5. Nullsafe Operator

Instead of checking if an object is null step by step, you can do this:

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

If any part is null, it returns null.

6. Attributes

Attributes replace PHPDoc comments for adding metadata to classes and functions:

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

7. Constructor Property Promotion

You can now write class properties in the constructor directly:

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

8. str_contains and str_starts_with

PHP 8 adds helper functions:

str_contains("Hello World", "World"); // true
str_starts_with("Hello World", "Hello"); // true

PHP 8.1 Features

1. Enums

Enums let you define a list of constant values:

enum Status {
    case Active;
    case Inactive;
}

2. Readonly Properties

You can make properties that cannot be changed after they are set:

class User {
    public readonly string $name;

    public function __construct($name) {
        $this->name = $name;
    }
}

3. Fibers

Fibers make it easier to manage asynchronous code and background tasks.


PHP 8.2 Features

1. Disjunctive Normal Form Types

This allows combining types using logic:

function test((A&B)|C $input) {}

2. Readonly Classes

You can now make whole classes readonly:

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

3. Deprecated Dynamic Properties

In older PHP, you could create properties by mistake. Now it is not allowed unless the class allows it.


Tools and Tips

  1. Use phpinfo() to see your current PHP version

  2. Use tools like Rector to upgrade old code

  3. Test new features in small demo apps

  4. Use type hints to make code more safe


Final Thoughts

If you stopped at PHP 5 or PHP 6, PHP 8 may look very different. But the core is still the same. New features are added to help you write better and more reliable code. You do not need to learn everything at once. Try using union types or named arguments in your next code update.

PHP has grown a lot, and it is a modern language now. Many large companies use PHP 8 for high-performance applications. Start exploring one feature at a time and enjoy the journey.

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.