PHP 8.0 Features

Category : Knowledge Share

PHP 8 has been officially released to the General Availability on November 26, 2020!

This new major update brings many optimizations and powerful features to the language. I will discuss the most interesting changes here.

Here are the main features :

  • PHP JIT (Just in Time Compiler)
  • New PHP Functions
  • PHP 8 Performance Benchmarks

PHP JIT(Just in Time Complier)

Among the major new features of PHP 8 is the JIT compiler, which improves performance significantly. Earlier PHP was not compiled but interpreted line by line. With JIT compiler (Just in Time), this is changed. It works by compiling parts of the code during runtime – and in doing so acts very much like a cached version of the code.

JIT for Live Web Apps

According to the JIT, the just in time compiler implementation should improve PHP performance. But would we really experience such improvements in real-life apps like WordPress?

The early tests show that JIT would make CPU workload run faster. However, the RFC warns like the previous attempts – it currently doesn’t seem to significantly improve real-life apps like WordPress. With JIT enabled, the code wouldn’t run by the Zend VM, but by the CPU itself, which would improve the calculation speed. Web apps like WordPress also rely on other factors like TTFB, database optimization, HTTP requests, etc and so we shouldn’t expect a significant boost in PHP execution speed when it comes to WordPress and similar apps.

New PHP Functions (For Developers)

1. str_contains
2. str_starts_with() and str_ends_with()
3. get_debug_type

1. str_contains

Before PHP 8, strstr and strpos were the typical options for developers to search for a word inside a given string. The problem is that both functions usage can be confusing for new PHP developers. See the following examples:

Earlier Method

Updated Method with PHP 8.0

2. str_starts_with() and str_ends_with() (New Function)

In addition to the str_contains function, two new functions allow to search for a word inside a given string: str_starts_with and str_ends_with
Example :
$str = “WordPress”;
if (str_starts_with($str, “Word”))  // outputs TRUE
if (str_starts_with($str, “word”)) //outputs  FALSE
if (str_ends_with($str, “Press”)) //outputs TRUE
if (str_ends_with($str, “press”)) //outputs  FALSE

Note : This is case sensitive. We will know it more when we will use it.

3. get_debug_type (New Function)

get_debug_type is a new PHP function that returns the type of a variable for the given input. The new function works in quite a similar way as the gettype function, but get_debug_type returns native type names and resolves class names.

Example :

The above example will output something similar to:

integer
double
NULL
object
string

The above example when used with get_debug_type() will return native types.

Example :

The above example will output something similar to:

int
float
null
class@anonymous
string

3. PHP 8 Performance Benchmarks

If you’re wondering how fast PHP 8 is, there is an answer. Kinsta benchmarked 20 PHP platforms/configurations on 7 different PHP versions (5.6, 7.0, 7.1, 7.2, 7.3, and 8.0).
PHP 8.0 emerged as the winner in most platforms that support it, including WordPress and Laravel.

 

Summary

In this post, we covered the most interesting optimizations and features coming with PHP 8. The most awaited of which is surely the Just in Time compiler, but there’s so much more with PHP 8.

For more information, check out this article.

Leave a Reply