PHP 8.0: An introduction to the new version of PHP

We are happy to announce that PHP 8.0 is now available on servers. This version comes with new features and changes that will bring the best possible performance to your web projects.

Since this is a major update, we encourage all users to migrate to the latest version and familiarize themselves with the new changes, which we will be covering in this article.

Why do you need PHP 8.0?

The latest benchmark tests performed by show that PHP 8.0 is performing a 10% better than its predecessors. These data suggest a promising future for PHP-based websites and applications.

Better yet, the test wasn’t even run with the JIT compiler, a new optimization feature introduced with PHP 8.0. So developers can expect much more optimal performance if it is enabled.

Additionally, the release implements new features to make coding much faster and cleaner, reducing the amount of boilerplate and redundant code.

Since this is a version update, it is likely that your website will experience changes that could take it down if you transition to PHP 8.0 without applying any prior modifications. To help you prepare for the migration, we’ll walk you through the latest features.

How to change your PHP version

customers can follow these steps to update their PHP version:

  1. Login in hPanel and open the panel hosting account.
  2. In the section AdvancedClick on PHP configuration.
  3. The eyelash PHP version will show which version is being used. To switch to the latest version, select PHP 8.0 and press Save.

Please note that the update may take a couple of minutes to complete and may make your website temporarily unavailable.

What’s new in PHP 8.0

There are many quality of life changes coming with the new update along with many new features. Let’s explore what’s new and changed in PHP 8.0.

JIT (Just-in-Time) compiler

The JIT compiler is one of the most exciting updates to PHP 8.0. This function is intended to work with opcache to improve performance when executing scripts.

What is JIT?

JIT, short for just in time, compiles the opcode to machine code just before executing it for output. To understand what that means and how it works, we need to understand the stages of PHP script execution, which are:

  • Lexical analysis. This step is where the Zend Engine, the PHP interpreter, translates the code into machine-readable tokens.
  • Analyzer. The interpreter parses the tokens to generate the abstract syntax tree (AST), a tree-like representation that shows how the code works.
  • Compilation. The interpreter converts the AST nodes into opcode, which is a machine-readable instruction that tells the Zend virtual machine (VM) what operation to perform.
  • Execution. The interpreter hands the opcode to the Zend VM, which will compile the opcode to machine code for execution.
See also  Key takeaways from exclusive webinar with Neil Patel: Learn how to monetize your blog

This process uses a significant amount of server resources, especially if a PHP script receives repeated requests.

That’s why PHP 5.5 introduced the extension opcachewhich stores the opcode from the compile stage.

When the server receives a new request for the same script, the interpreter can immediately execute the opcode from the opcache. That way, you don’t have to restart the execution process from the beginning.

added a function preload several years later, to convert the pre-compiled opcache scripts into opcode during startup. As a result, the interpreter can immediately deliver the opcode for execution when the server first receives a request for the script.

Despite these advantages, there are several disadvantages. One is that towards the end of the process, the Zend VM still needs to convert the opcode to machine code before executing it, which can be time- and resource-intensive.

That’s where the JIT compiler comes in. It will compile the opcode into machine code during its first run to prepare for the next run.

When there is a request for a JIT-compiled script, PHP will execute it directly by the CPU instead of the Zend VM, resulting in faster performance. This is what the script execution stages will look like in PHP 8.0, compared to the previous version:

There are two JIT compilation engines:

  • Function. This approach will identify and compile a complete function without figuring out which parts are frequently called.
  • Tracking. This mode will only parse and compile the most used parts in the function to save time and memory. This is the default engine in PHP 8.0.

What does JIT mean in a nutshell?

According to, enabling JIT is the best way to increase PHP performance. Therefore, bypassing this feature may result in you missing out on an important benefit.

Fortunately, recent tests show that the JIT compiler has been able to improve the script execution process, mainly if the tracing engine is used.

Synthetic benchmarks reveal a three times speed increase after enabling Follow mode. In long-running applications, you can expect to see a performance improvement of up to two times.

See also  What is a DBMS: Complete guide on database management systems

For WordPress users and developers, the JIT compiler can also add a slight boost, although it may not be as significant.

You will need to lower the TTFB, optimize the database, and reduce the number of HTTP requests to get the best possible performance. With that said, PHP developers can continue improvement efforts through the use of profiling and speculative optimizations.

If you want to enable JIT, make sure the extension opcache be active.

Customers with a can do this by opening the panel on the hosting account -> PHP configuration. in the tab PHP extensionsmake sure to check the box «opcache«.

New features in PHP 8.0

There are tons of cool features besides JIT. In this section, we provide an overview of the major additions and changes in PHP 8.0.

Join Types 2.0

In many cases, a function can use more than one type, but it was not possible to specify this in earlier versions of PHP unless you declared the types using DocComments.

This is an example of what it would look like:

class Number { /** * @var int|float $number */ private $number; /** * @param int|float $number */ public function setNumber($number) { $this->number = $number; } /** * @return int|float */ public function getNumber() { return $this->number; } }

Earlier versions of PHP introduced two special binding types: Nullable (using the syntax ?Type) and Iterable (for array Y Traversable).

However, what was missing was native support for arbitrary join types, which is a feature that comes with PHP 8.0. Now, you can simply write the types that the function can use and separate them using the syntax T1|T2|…So:

class Number { private int|float $number; public function setNumber(int|float $number): void { $this->number = $number; } public function getNumber(): int|float { return $this->number; } }

Note that the example no longer includes @var, @param either @returnwhich makes the code much cleaner.

You can use union types for properties, arguments, and return types, although there are some limitations to be aware of. See the for more information.

name arguments

In earlier versions of PHP, passing multiple arguments to a function required using the order positions in which the parameters were declared, like so:

array_fill(0, 100, 50);

One challenge with this is that you may not remember the order of the parameters. Also, it can be difficult to understand what each refers to when you revisit the code.

See also  How to delete files and folders in Linux

With PHP 8.0, you have the option to add a name next to the parameter so you can pass it to a function using its name instead. This is how it usually looks:

// Using positional arguments: array_fill(0, 100, 50); // Using named arguments: array_fill(start_index: 0, num: 100, value: 50);

An advantage of this feature is that by learning what each parameter does, the overall process will be much faster.

Also, named arguments are order-independent, so you don’t have to remember the order positions of each parameter in your declarations. Therefore, the following example will have the same meaning as the previous one:

array_fill(value: 50, num: 100, start_index: 0);

It is also possible to mix name and positional arguments, as long as the named ones come second. In other words, the following code is acceptable:

test($foo, param: $bar);

On the other hand, this code will result in an error:

test(param: $bar, $foo);

Finally, with name arguments, you only need to write parameters whose default values ​​you want to override. Feel free to skip the ones that have default values ​​that you’d like to keep. Here is an example in the :

htmlspecialchars($string, default, default, false); // vs htmlspecialchars($string, double_encode: false);

The standard syntax for name arguments is ParameterName: $value. Do not type the name dynamically, as illustrated below, as this will cause an error.

// Unadmitted. function_name($variableStoringParamName: $value);

Match expressions

A match expression is similar to a switch statement, in that its purpose is to compare multiple values. However, semantics is much more efficient and less error-prone.

Consider the following example of a change statement:

switch ($this->lexer->lookahead) { case Lexer::T_SELECT: $statement = $this->SelectStatement(); break; case Lexer::T_UPDATE: $statement = $this->UpdateStatement(); break; case Lexer::T_DELETE: $statement = $this->DeleteStatement(); break; default: $this->syntaxError(‘SELECT, UPDATE or DELETE’); break; }

With a match expression, the same statement will look much shorter:

$statement = match ($this->lexer->lookahead) { Lexer::T_SELECT => $this->SelectStatement(), Lexer::T_UPDATE => $this->UpdateStatement(), Lexer::T_DELETE => $ this->DeleteStatement(), default => $this->syntaxError(‘SELECT, UPDATE or DELETE’), };

The code block above shows that a match expression can return a value. This is different from a switch statement where…

Loading Facebook Comments ...
Loading Disqus Comments ...