# PHP 8: What you need to know about the latest version

PHP is one of the programming languages that is easiest to learn. With a [PHP tutorial](https://www.ionos.com/digitalguide/websites/website-creation/learn-php-our-all-encompassing-php-tutorial-for-beginners/ "Learn PHP: our all-encompassing PHP tutorial for beginners") and the right motivation, you can write the first scripts and execute commands within a few hours. [PHP 7](https://www.ionos.com/digitalguide/websites/web-development/how-php7-speeds-up-the-internet/ "How PHP7 speeds up the internet") introduced speed optimization, which led to an improved ranking. Now the open-source language has received a **major update to version 8.0**. A year later, version 8.1 is available.

---

### Tip

IONOS offers [flexible and secure PHP web hosting](https://www.ionos.com/hosting/php-web-hosting "PHP Web Hosting") for private users and companies of any size. If your needs grow, you can easily switch to a bigger package with just a few clicks.

---

PHP 8.1 has now been released in beta and is currently being tested. The final update won’t follow until **November 2021**, but the new features are already known. You can expect the following new features:

- [Enum](https://wiki.php.net/rfc/enumerations "Official RFC on Enums"): So-called enumerations (enumerations with a specified number of values) are now available as a variable type.
- [Fibers](https://wiki.php.net/rfc/fibers "Official RFC on Fibers"): The new code block type Fibers gives you the ability to run multiple program threads in parallel.
- [Arrays](https://wiki.php.net/rfc/array_unpacking_string_keys "Official RFC on Array unpacking"): Since PHP 7.4, it’s been possible to unpack arrays with numeric values. Now this is also possible for arrays with strings.
- [Readonly](https://wiki.php.net/rfc/readonly_properties_v2 "Official RFC on Readonly"): Class properties can be marked as 'readonly' from now on and are thus no longer modifiable.
- [Intersection](https://wiki.php.net/rfc/pure-intersection-types "Official RFC on Intersection Types"): Intersection Types (basically the counterparts to Union Types) are now available.
- [Never](https://wiki.php.net/rfc/noreturn_type "Official RFC on never return type"): The new return value 'never' indicates that a function will terminate and return no other value.
- [Final](https://wiki.php.net/rfc/final_class_const "Official RFC on final flag"): You can now mark class constants with 'final' so they can’t be changed by subclasses.
- [Fsync](https://wiki.php.net/rfc/fsync_function "Official RFC on fsync"): PHP 8.1 introduces the two functions 'fsync' and 'fdatasync' to synchronize data from the buffer to the storage medium.
- [Octal](https://wiki.php.net/rfc/explicit_octal_notation "Official RFC on octal prefix"): 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](https://wiki.php.net/rfc/restrict_globals_usage "Official RFC on $GLOBALS"): The use of '$GLOBALS' has been slightly restricted and now results in an error message in some cases.
- [Null](https://wiki.php.net/rfc/deprecate_null_to_scalar_internal_arg "Official RFC on Null"): Internal functions will no longer accept null values on non-nullable arguments.

---

### Note

This is just a sampling of the most interesting changes in PHP 8.1. Many more planned new features can be found on the [official Wiki with all Request for Comments](https://wiki.php.net/rfc "Wiki on PHP").

---

## PHP 8 release in November 2020

On **26 November 2020**, PHP 8.0.0 - the latest edition of the popular scripting language - was released. This corresponds to the normal **three-year cycle** for PHP. The predecessor PHP 7.4 was around for about a year after [support for PHP 7.1](https://www.ionos.com/digitalguide/websites/web-development/php-71/ "PHP 7.1") was discontinued. 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 technically possible, it is not recommended. **Keeping your website code up-to-date** and switching to new versions of PHP has various advantages: New functions offer more variety, performance 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](https://www.ionos.com/hosting/web-hosting "Web hosting - IONOS") now. You can then change the PHP version used in your [IONOS customer account](https://www.ionos.com/help/hosting/managing-php-versions-in-the-control-panel/display-or-change-php-versions/ "Change PHP version in IONOS account") and switch to PHP 8 or PHP 8.1 (as soon as it’s available).

---

## 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 compatible. However, most of the changes that could lead to complications were already addressed in versions [7.2](https://wiki.php.net/rfc/deprecations_php_7_2 "Deprecations for PHP 7.2"), [7.3](https://wiki.php.net/rfc/deprecations_php_7_3 "Deprecations for PHP 7.3"), and [7.4](https://wiki.php.net/rfc/deprecations_php_7_4 "Deprecations for PHP 7.4").

The latest changes include:

- The *real* type
- Magic quotes legacy
- *array\_key\_exists()* with objects
- *FILTER\_SANITIZE\_MAGIC\_QUOTES* filter
- Reflection *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 without backward compatible changes. A **complete list of changes** can be found on the [PHP project site](https://www.php.net/releases/8.0/en.php "php.net: PHP 8.0.0 Release Announcement").

---

### Note

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

---

## These are the new PHP 8 features

The new version of PHP has some new features that provide web developers with lots of additional possibilities. Here we have summarized the most important adjustments compared to the predecessors.

---

### Note

On **November 30, 2020**, the [PHP 7.2](https://www.ionos.com/digitalguide/websites/web-development/php-72/ "PHP 7.2") version reached its official **end-of-life (EOL) date** and will no longer be supported. For more on this, see our article on this topic.

---

### JIT Compiler

Among the major new features of PHP 8 is the **JIT compiler**, which improves performance significantly. PHP is not compiled but **interpreted line by line**. The JIT compiler (Just in Time) works by compiling parts of the code during runtime - 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](https://levelup.gitconnected.com/how-fast-is-php-8-going-to-be-f7fdc111cd6 "Test report on JIT Compiler"). 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 calculated the average time.

The following average values were the results of his testing:

<table>
  <thead>
    <tr>
      <th>Version</th>
      <th>Time in Seconds</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>5.3</td>
      <td>0.64574003219604</td>
    </tr>
    <tr>
      <td>7.4</td>
      <td>0.10253500938416</td>
    </tr>
    <tr>
      <td>8 (without JIT)</td>
      <td>0.098223924636841</td>
    </tr>
    <tr>
      <td>8 (with JIT)</td>
      <td>0.053637981414795</td>
    </tr>
  </tbody>
</table>

Even if changing from version 7.4 to 8 without JIT does not make much difference, the difference to version 8 with JIT is significant. The JIT Compiler **improves performance 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:

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

However, there is one restriction, 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:

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

### Static return type

*Static* is a special class name and becomes a valid return type in the new version besides *self* &amp; *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 references the entity classes of the objects. This leads to resource-saving handling of the objects. An example from the documentation is as follows:

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

### Use ::class for objects

In earlier versions, you had to use get\_class() to assign a class to objects. But now, you can apply ::*class* to objects. This ensures that the source code is kept slimmer.

### Stringable interface

The Stringable interface is automatically added to classes that implement the *\_\_toString()* method. Previously, this step had to be done **manually**. The code looks like this:

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

### fdiv

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

### Type errors

Previously, only user-defined functions triggered *TypeErrors*. Internal functions issued a *warning* and *null.* In PHP 8 most internal functions now also return a type error.

### New classification for engine warnings

Till 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](https://wiki.php.net/rfc/engine_warnings "Official listing of warnings at PHP") can be found in the documentation.

### Correct signature of magic methods

In previous PHP versions, it was possible to write magic methods with signatures that did not match the expected signature, for example *\_\_clone(): float* or *\_\_isset(): Closure*. Since the introduction 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 signatures are correct.

### Reflection

The Reflection signatures have been changed. Originally, Reflection was specified as follows:

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

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

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

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

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

---

### Tip

You haven’t got your own website up and running yet? You can expect a lot of flexibility and the best performance with [virtual servers](https://www.ionos.com/servers/vps "Cheaply rent a vServer - Windows or Linux - IONOS") from IONOS. Choose the solution that best fits your project.

---


This is a markdown version of: [https://www.ionos.com/digitalguide/websites/web-development/php-8/](https://www.ionos.com/digitalguide/websites/web-development/php-8/) for AI/LLM consumption.