close icon
PHP

What's New in PHP 8

Learn what's new and what has changed in the new PHP 8 release.

November 19, 2020

The long-anticipated GA release of PHP 8.0 is scheduled for November 26th, 2020. Let's take a look at a few of the new additions in PHP 8 and who is supporting it at release.

New Additions

Union types

PHP 8 now supports union types, which allow you to declare more than one type in your classes or arguments. Before version 8, PHP already supported two special union types: nullable and iterable.

// Nullable - return type can be string or null
function getUsername() : ?string {
  return $this->username;
}

// Iterable pseudo-type - return type can be array or
// instance of Traversable
function iterate() : iterable {
  // ...
}

In PHP 8, these special union types have been extended to include several combinations, with a few exceptions.

// Union - return type can be int or float
function getNumber() : int|float {
  return $this->number;
}

// Nullable implemented as new union type
function getUsername() : string|null {
  return $this->username;
}

mixed Types

PHP 8 introduces a new pseudo-type to the PHP type system: mixed. This pseudo-type allows you to declare a value of any type without excluding type information altogether.

From the Mixed Type RFC:

An explicit mixed type would allow people to add types to parameters, class properties, and function returns to indicate that the type information wasn't forgotten about, it just can't be specified more precisely, or the programmer explicitly decided not to do so.

To get an idea of all types that mixed type includes, here's mixed represented as a union type:

array|bool|callable|int|float|null|object|resource|string

JIT (Just in Time compiler)

In a compiled language, the source code is converted to computer-readable code ahead of time. Interpreted languages, on the other hand, convert the source code as it's executed, which is much slower. PHP is an interpreted language, and there have been several tactics used to improve PHP performance in the past.

The latest step in speeding up performance is the addition of the JIT compiler in PHP 8.

The JIT compiler is sort of a middle ground between compilation and interpretation. It will compile and cache some sections of code at runtime so that the compiled version can be used instead of the interpreted version. This could lead to huge performance gains for PHP, but with some caveats. Generally, JIT compilers mostly benefit CPU-intensive applications, such as 3D rendering or large mathematical computations. If you're using PHP for web applications, you may not see a substantial performance boost by enabling the JIT compiler.

👩‍💻 Note: JIT is not enabled by default. Check out this excellent guide about how to enable JIT in PHP 8.

str_contains function

The new str_contains function can be used to check if a given string contains another string.

str_contains ( string $haystack , string $needle ) : bool

Example:

str_contains("PHP is cool!", "PHP"); // true 
str_contains("PHP is cool!", "php"); // false 
str_contains("PHP is cool!", "not"); // false

👩‍💻 Note: str_contains() is case-sensitive

Named Arguments

Named arguments are another new addition to PHP 8. With named arguments, you can now pass an argument to a function based on the parameter name. This means that the order in which arguments are passed won't matter, as the name will be used to determine the parameter.

Example

createUser(id: 1, username: hollylawly);
// position of argument doesn't matter
createUser(username: hollylawly, id: 1);

match Expressions

The match expression introduced in PHP 8 is similar to the switch statement, but with a few improvements:

  • match evaluates to the result of the expression, so you don't need to remember to set some variable at every case
  • No type coercion like in switch statements
  • Implicit break is added after every arm, so if you forget to add a break, you won't experience fallthrough

Example

$operation = 'write';

// Switch statement
switch ($operation) {
  case 'read':
    $message = 'Viewed!';
    break;
  case 'write':
    $message = 'Created!';
    break;
  case 'update':
    $message = 'Updated!';
    break;
  case 'delete':
    $message = 'Deleted!';
    break;
}

// Equivalent match expression
$message = match ($operation) {
  'read' => 'Viewed!',
  'write' => 'Created!',
  'update' => 'Updated!',
  'delete' => 'Deleted!',
}

👩‍💻 Note: match is now a reserved keyword

Trailing commas in parameters lists

This one is pretty straightforward but bound to make a lot of people happy! Like argument lists, you can now also leave the trailing comma off at the end of parameter lists.

function myFunction(
  param1,
  param2,
  param3,
) {
  // ...
}

An additional bonus of this is that it simplifies diffs where a parameter is added. Instead of two lines changing (adding a comma to the last parameter and the line with the new parameter), only one line would be highlighted.

// --- Diff with no trailing comma
// Before
function myFunction(
  param1,
  param2,
  param3
) {
  // ...
}

// After
function myFunction(
  param1,
  param2,
  param3,  // 👈 this line changed
  param4   // 👈 this line changed
) {
  // ...
}

// --- Diff with trailing comma
// Before
function myFunction(
  param1,
  param2,
  param3,
) {
  // ...
}

// After
function myFunction(
  param1,
  param2,
  param3,
  param4,   // 👈 this line changed
) {
  // ...
}

PHP 8 Support

Because PHP 8 has been on the horizon for so long, a lot of community work has already been done to make sure that it's supported across the web. But before you upgrade, you should make sure it's being supported wherever you use it.

Here is the current support status of some popular projects, frameworks, applications, and more.

WordPress

WordPress plans to support PHP 8 in their next release (v5.6), which is currently scheduled for December 8th, 2020. This doesn't affect the minimum required version, which is currently PHP 5.6.2.

Because this is a major version change, a lot of testing is required. If you're interested in helping out the WordPress community with PHP 8 testing, be sure to check out their call for testing.

Laravel

The Laravel team completed all updates needed for PHP 8 support well ahead of time. This includes updates to the following projects:

For a full list, check out Laravel's Full PHP 8.0 Support PR.

Auth0

The Auth0 PHP SDK has been updated to fully support PHP 8 right out of the gate, so you can get up and running with it immediately!

If you're interested in an easy-to-use solution for adding authentication and authorization to your PHP application, be sure to sign up for a free Auth0 account to get started.

With Auth0, you only have to write a few lines of code to get:

To get up and running quickly, check out the PHP Quickstart.

Symfony

The Symfony team is ready to support PHP 8 right out of the gate. If you're curious about the process of supporting PHP 8 in Symfony, be sure to check out this video where Nikita Popov of JetBrains interviews Nikolas Grekas, Symfony's principal engineer.

"We are quite happy. It went very smoothly. PHP 8 is pretty easy to support." — Nicolas Grekas

Wrap Up

A lot of work has gone into the release of PHP 8, and the PHP community is no doubt excited to get their hands on it.

For a full list of changes or to get some context on why something was introduced, be sure to check out the implemented RFCs for PHP 8.

Let me know in the comments below what new features you're most excited about and if you plan to upgrade right out of the gate. Thanks for reading!

  • Twitter icon
  • LinkedIn icon
  • Faceboook icon