PHP 7.4 Guide: Performance, Features, Deprecations

PHP 7.4 has finally arrived! This new version, released on November 28, 2019, is now available on all . Developers can expect improvements in code readability, maintainability, and usability. Let’s look at some of the new features, performance tweaks, and other reasons why you should migrate to PHP 7.4.

What does PHP 7.4 bring for you?

PHP continues to evolve, releasing its latest PHP 7.4 update, packed with new features. As we have seen in previous versions of PHP 7, performance and speed continue to improve. One of the most exciting new features is the preload. It helps speed up script execution, as well as introduces the ability to have faster, cleaner code by simplifying common lines of code.

The good folks behind PHP have listened to their audience’s comments and requests and responded with full force. Since then, they have been continually changing the code to make it more intuitive and easier to switch between programming languages.

PHP is used on more than 78.9% of all websites. According to , the most popular sites that use PHP are Wikipedia, Pinterest, and Facebook, to name a few.

If we look specifically at WordPress sites running PHP, comparing PHP 5 and 7, we can see a two-fold increase in speed. WordPress powered websites definitely get the most out of using the latest version of PHP out there. users can reach new levels with the click of a button.

See all those cool figures? The chart shows some truth about websites that actively use PHP. do live websites are enough to grab your attention? That’s how many people use it right now. Additionally, PHP 7.4 is already performing better than PHP 7., with improved performance and other quality of life improvements.

The following graph shows the in new and old versions of PHP. Some of the criteria tested were ease of use, speed, and performance, among others.

How to change your PHP version

Ready to upgrade? It sure does, which is why makes it as easy as ever with these four easy steps. You’ll be testing out your new and improved version of PHP in no time.

  1. Login to your account and press the button Start.
  2. On your home page, scroll down to the section hosting and click on the icon Manage.
  3. In the search box, type PHP configuration and click on the corresponding option.
  4. Select PHP 7.4 and click Save.

Congratulations! You now have the best and most up-to-date version of PHP out there.

See also  WordPress Hosting | Safe, Fast and Best Price

To check your current version of PHP, all you need to do is go to the tab hosting and check the left side panel to see the PHP version. If it is less than 7.4, follow the steps and update it.

What’s new in PHP 7.4?

Since 2016, PHP7 has been releasing annual updates without fail. Every year they offer new features, additions, and the ability to write cleaner code, making the language more reliable and easier to use for those running it on their websites.

Let’s dive in and take a closer look at some of the changes that were made in the PHP 7.4 update. For a full list, see the changelog .

preload

Let’s talk code. When you use a framework or libraries, your files must be loaded and linked on every request. Preloading is when you can load frameworks and libraries into OPCache. This allows the server to load PHP files, store them in memory during startup, and make them available for future requests. Speaking of getting things up and running fast!

The preload is executed by a directive php.ini specific: opache.preload, which has the PHP script compiler and is executed when the server starts. It can also be used to preload more files and choose to include or compile them.

This is incredible; however, if the source of the preloaded files is ever changed, the server must be restarted. Preloaded files also remain cached in OPCache memory forever.

But, these preloaded files will still be available for future requests in case you need to use them again.

Spread operator in array expressions

When PHP 5.6 was released PHP started to support argument unpacking (spread operator) but now with version 7.4 we can use this function with an array expression. Argument unpacking is a syntax for unpacking arrays and traversables into argument lists. And, to do so, it just needs to be preceded by … (3 points). That is all.

Let’s look at this example:

$animals = ; $animalkingdom = ; // Result: ;

We can now expand an array from anywhere we want in another array, simply by using the spread operator syntax.

Here is a longer example:

$num1 = ; $num2 = ; // $num3 = ; // $num4 = array(…$num1, …$num2, 111); // $num5 = ; //

Not only that, but you can also use it in a function. Look at this example:

function getNum() { return ; } $num6 = ; // $num7 = )]; // function arrGen() { for($i = 11; $i < 15; $i++) { yield $i; } } $num8 = ; //

See also  10 WordPress RSS Feed Plugins to Easily Share the Latest Content

Additionally, you can now unpack arrays and generators returned from a function directly into a new array.

A code example would look like this:

function getAnimals(){ return ; } $num1 = ;

And with PHP 7.4, it would print:

array(6) { => string(3) “dog” => string(3) “cat” => string(8) “elephant” => string(4) “lion” => string(5) “tiger” => string(7) “giraffe” }

With this new array expression, the spread operators should perform much better than in 7.3 array_merge. This is because the spread operator is a language structure, while array_merge it is a function. Also, because the spread operator admits objects which implement traversable and array_merge only supports arrays.

Some important things to note, you can only use indexed arrays as string keys are not supported. If you use them, you will see a recoverable error on the screen, once it finds the chain key.

Another glorious benefit of version 7.4 is the removal of array_merge. Say goodbye to the dreaded index change!

For example, let’s look at this long organized array combination below:

$array = ; $array = ‘orange’; $array = ‘apple’; //shifting var_dump($array); // prints array(3) { => string(6) “banana” => string(5) “apple” => string(6) “orange”

Another benefit of 7.4 is the use of the generator function. A It operates like a normal function, except that instead of returning a value, a generator function produces as many values ​​as needed.

Analyze the sample code below:

function generator() { for ($i = 3; $i <= 5; $i++) { yield $i; } } $num1 = ;

weak references

PHP 7.4 now has a WeakReference class, not to be confused with the class WeakRed or the extension Weakref.

WeakReferences allows the programmer to remember a reference to an object. This is useful because it doesn’t prevent the object from being destroyed. It is ideal for implementing cache-like structures.

WeakReference { /* Methods */ public __construct ( void ) public static create ( object $referent ) : WeakReference public get ( void ) : ?object }

Contravariant parameters and covariant returns

Currently, PHP mainly uses parameter types and invariant return types. That is, if a method has a parameter or return type X, then the subtype parameter or return type must also be of type X.

Now, PHP 7.4 proposes to allow covariants (ordered from specific to generic) and contravariants (reversing the order) in the types of parameters and returns.

Here is an example of both:

Covariant return type example:

interface Factory { function make(): object; } class UserFactory implements Factory { function make(): User; }

Contravariant parameter type example:

interface Concatable { function concat(Iterator $input); } class Collection implements Concatable { // accept all iterables, not just Iterator function concat(iterable $input) {/* . . . */} }

See also  How to Minify HTML, CSS or JavaScript in WordPress

Property Types 2.0

Since PHP 5, type hints have been an available feature that allows you to specify the type of variable that is expected to be passed to a function or class. In PHP 7.2 migrations, adding the data type objects gave hope that more of this style would be available in the future. The future is now.

In the new 7.4, PHP can support the following list of types:

bool, int, float, string, array, object, iterable, self, parent any class or interface name ?type // where “type” can be any of the above

Note that the type parent can be used in classes and you don’t need to have a parent consistent with the parameter and return type.

Also, keep in mind that they do not allow void neither callable. It was deleted void because it was not useful and had unclear semantics; callablebecause its behavior depended on the context.

Let’s look at some more examples.

Here’s a class, written for PHP 7.3:

class User { /** @var int $id */ private $id; /** @var string $name */ private $name; public function __construct(int $id, string $name) { $this->id = $id; $this->name = $name; } public function getId(): int { return $this->id; } public function setId(int $id): void { $this->id = $id; } public function getName(): string { return $this->name; } public function setName(string $name): void { $this->name = $name; } }

In PHP 7.4, without sacrificing any security, a class can now be written as simple as:

class User { public int $id; public string $name; public function __construct(int $id, string $name) { $this->id = $id; $this->name = $name; } }

Here are some examples of all the types that version 7.4 now supports:

class Example { public int $scalarType; protected ClassName $classType; private ?ClassName $nullableClassType; // Types are also legal on static properties public static iterable $staticProp; // Types can also be used with the “var” notation var bool $flag; // Typed properties may have default values ​​(more below) public string $str = “foo”; public ?string $nullableStr = null; // The type applies to all properties in one declaration public float $x, $y; // equivalent to: public float $x; public float $y; }

Arrow Functions (arrow) 2.0

Anonymous functions in PHP tend to be lengthy, even when they only perform simple operations. This is because…

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