PHP is one of the pro­gram­ming languages that is easiest to learn. With a PHP tutorial and the right mo­tiv­a­tion, you can write the first scripts and execute commands within a few hours. PHP 7 in­tro­duced speed op­tim­isa­tion, which led to an improved ranking. Now the open-source language has received a major update to version 8.0. A year later, the smaller update to 8.1 followed.

PHP 8.1 was released on 25 November 2021. These are the new features it brought:

  • Enum: So-called enu­mer­a­tions (enu­mer­a­tions with a specified number of values) are now available as a variable type.
  • Fibers: The new code block type Fibers gives you the ability to interrupt functions and resume them later, so that multiple program threads can run con­cur­rently (not in parallel).
  • Arrays: Since PHP 7.4, it’s been possible to unpack arrays with numeric keys. Now this is also possible for arrays with string keys.
  • Readonly: Class prop­er­ties can be marked as ‘readonly’ from now on and are thus no longer modi­fi­able.
  • In­ter­sec­tion: In­ter­sec­tion Types (basically the coun­ter­parts to Union Types) are now available.
  • Never: The new return type ‘never’ indicates that a function never returns, but instead throws an exception or ends the script, for example.
  • Final: You can now mark class constants with ‘final’ so they can’t be changed by sub­classes.
  • Fsync: PHP 8.1 in­tro­duces the two functions ‘fsync’ and ‘fdatasync’ to syn­chron­ise data from the buffer to the storage medium.
  • Octal: To indicate the octal number system, in addition to ‘0’ (zero), the prefix ‘0o’ (zero and small O) can be used in the future.
  • Globals: The use of ‘$GLOBALS’ has been slightly re­stric­ted and now results in an error message in some cases.
  • Null: Passing null values to non-nullable arguments of internal functions is now de­prec­ated and triggers a de­prec­a­tion notice.
Note

This is just a sampling of the most in­ter­est­ing changes in PHP 8.1. Many more new features can be found on the official Wiki with all Request for Comments.

PHP 8 release in November 2020

On November 26th 2020, PHP 8.0.0 - a new major version of the popular scripting language - was released. The pre­de­cessor PHP 7.4 was around for about a year after support for PHP 7.1 was dis­con­tin­ued. Support for PHP 7.2 also ended at the end of 2020. Many websites developed in PHP still rely on the old versions. Although this is tech­nic­ally possible, it is not re­com­men­ded. Keeping your website code up-to-date and switching to new versions of PHP has various ad­vant­ages: New functions offer more variety, per­form­ance can be massively increased, and security gaps are closed.

Try out PHP 8 now!

Try PHP 8 now! You aren’t an IONOS customer yet? Then book one of our cheap web hosting offers now. You can then change the PHP version used in your IONOS customer account and switch to PHP 8.

What are the problems with the old code?

Because PHP 8 is a major new release, one should expect old code to no longer be com­pat­ible. However, most of the changes that could lead to com­plic­a­tions were already addressed in versions 7.2, 7.3, and 7.4.

The latest changes include:

  • The real type
  • Magic quotes legacy
  • array_key_exists() with objects
  • FILTER_SANITIZE_MAGIC_QUOTES filter
  • Re­flec­tion export() methods
  • mb_strrpos() with encoding as 3rd argument
  • implode() parameter order mix
  • Unbinding $this from non-static closures
  • hebrevc() function
  • convert_cyr_string() function
  • money_format() function
  • ezmlm_hash() function
  • restore_include_path() function
  • allow_url_include ini directive

If you have always kept your code up to date, you won’t have any problems migrating to PHP 8, even if the new update is a major release with backward-in­com­pat­ible changes. A complete list of changes can be found on the PHP project site.

Note

Microsoft has announced that it will no longer provide Windows support for PHP 8 and later versions. However, PHP de­velopers have already announced their intention to close this loophole them­selves.

These are the new PHP 8 features

The new version of PHP has some new features that provide web de­velopers with lots of ad­di­tion­al pos­sib­il­it­ies. Here we have sum­mar­ised the most important ad­just­ments compared to the pre­de­cessors.

Note

On 30th November 2020, the PHP 7.2 version reached its official end-of-life (EOL) date and has not been supported since.

JIT Compiler

Among the major new features of PHP 8 is the JIT compiler, which improves per­form­ance sig­ni­fic­antly. PHP first trans­lates source code into in­ter­me­di­ate code (opcodes), which the Zend Engine then executes. The JIT compiler (Just in Time) works by trans­lat­ing parts of the code at runtime directly into machine code - and in doing so acts very much like a cached version of the code.

This new feature in PHP 8 has already been tested by Pedro Escudero. He used a simple script to compare versions 5.3, 7.4, and 8 (with and without JIT). For this purpose, he ran the script 100 times in each version and then cal­cu­lated the average time.

The following average values were the results of his testing:

Version Time in Seconds
5.3 0.64574003219604
7.4 0.10253500938416
8 (without JIT) 0.098223924636841
8 (with JIT) 0.053637981414795

Even if changing from version 7.4 to 8 without JIT does not make much dif­fer­ence, the dif­fer­ence to version 8 with JIT is sig­ni­fic­ant. The JIT Compiler improves per­form­ance by more than 45 percent.

Union Types

Union Types are known from other languages like C/C++, TypeScript or Haskell. Here two or more types can form a union and any of the types mentioned can be used. In the code itself this can look like this:

public function foo(Foo|Bar $input): int|float;
php

However, there is one re­stric­tion, because void cannot become part of a union type; it does not output a return value. Also, nullable unions can be declared with |null or ? as in this example:

public function foo(Foo|null $foo): void;
public function bar(?Bar $bar): void;
php

Static return type

Static is a special class name and becomes a valid return type in the new version besides self & parent.

WeakMap

WeakRefs were already added in PHP 7.4. With PHP 8, however, WeakMaps have been included which are the extension of this function. WeakMaps and WeakRefs can be used to delete objects when only the cache ref­er­ences the entity classes of the objects. This leads to resource-saving handling of the objects. An example from the doc­u­ment­a­tion is as follows:

class FooBar {
    private WeakMap $cache;
    public function getSomethingWithCaching(object $obj) {
        return $this->cache[$obj] ??= $this->computeSomethingExpensive($obj);
    }
    // ...
}
php

Use ::class for objects

In earlier versions, you had to use get_class() to get the class name of an object. But now, you can apply ::class to objects. This ensures that the source code is kept slimmer.

Stringable interface

The Stringable interface is auto­mat­ic­ally added to classes that implement the __toString() method. Pre­vi­ously, this step had to be done manually. The code looks like this:

class Foo
{
    public function __toString(): string
    {
        return 'foo';
    }
}
function bar(string|Stringable $stringable) { /* … */ }
bar(new Foo());
bar('abc');
php

fdiv

Using the new fdiv() function, division with 0 is allowed. You now get INF, -INF or NAN as return value.

Type errors

Pre­vi­ously, only user-defined functions triggered TypeEr­rors. Internal functions issued a warning and null. In PHP 8 most internal functions now also return a type error.

New clas­si­fic­a­tion for engine warnings

Until PHP 8, there were lots of errors that only issued a warning or note. This has now been updated. A full listing of the new PHP error messages can be found in the doc­u­ment­a­tion.

Correct signature of magic methods

In previous PHP versions, it was possible to write magic methods with sig­na­tures that did not match the expected signature, for example __clone(): float or __isset(): Closure. Since the in­tro­duc­tion of PHP 7.0, the correct use of magic methods has only been randomly checked. Various parameter and return value checks have been added to the new version to ensure that the sig­na­tures are correct.

Re­flec­tion

The Re­flec­tion sig­na­tures have been changed. Ori­gin­ally, Re­flec­tion was specified as follows:

ReflectionClass::newInstance($args);
ReflectionFunction::invoke($args);
ReflectionMethod::invoke($object, $args);
php

However, in PHP 8 it is now written like this:

ReflectionClass::newInstance(...$args);
ReflectionFunction::invoke(...$args);
ReflectionMethod::invoke($object, ...$args);
php

To support PHP 7 & PHP 8 at the same time, you should proceed as follows:

ReflectionClass::newInstance($arg = null, ...$args);
ReflectionFunction::invoke($arg = null, ...$args);
ReflectionMethod::invoke($object, $arg = null, ...$args);
php
Tip

You haven’t got your own website up and running yet? You can expect a lot of flex­ib­il­ity and the best per­form­ance with virtual servers from IONOS. Choose the solution that best fits your project.

Reviewers

Go to Main Menu