PHP 7.4 has finally arrived! This new version, released November 28, 2019, is now available on all Hostinger servers. Developers can expect improvements in code readability, maintenance, and ease of use. Let’s look at some of the new features, performance tweaks and other reasons why you should migrate to PHP 7.4.
Table of Contents:
What Does PHP 7.4 Mean for You?
PHP continues to evolve, by releasing their newest PHP 7.4 update, full of new features. Like we have seen in previous PHP 7 releases – performance and speed keep improving. One of the most exciting new features is preloading. It helps speed-up script execution as well as introducing the ability to have faster and cleaner code, thanks to the simplification of common lines of code.
The good people responsible for PHP have heard their audience’s comments and requests and answered them in full force. They have since been continuously changing code to be more intuitive and easier to switch between programming languages.
PHP is used in over 78.9% of all websites. According to W3techs, the most popular sites using PHP are Wikipedia, Pinterest, and Facebook to name a few.
If we specifically look at WordPress sites running PHP, comparing PHP 5 and 7, we can see a double speed increase. WordPress powered websites definitely gain the most by using the latest PHP version out there. Hostinger users can super-charge their WordPress sites to new heights with just a click of a button.
See all these cool figures? This graph is spitting some truth about websites actively using PHP. Are 39,191,714 live websites enough to grab your attention? That’s how many are using PHP right now. Plus PHP 7.4 is already testing better than PHP 7.3 with improved performance and other quality of life improvements.
The graph below shows the overall benchmark test on new and old versions of PHP. Some of the criteria tested were ease of use, speed, and performance among others.
Changing Your PHP Version
Ready to update? Thought so. Hostinger makes it as easy as ever with these four simple steps. You’ll be fiddling around with your new-and-improved PHP version in no time.
- Log into your Hostinger account and hit the Home button.
- On your Home page, scroll down to the Hosting section and click on the Manage icon.
- In the search box, type in PHP configuration and click on it.
- Select PHP 7.4 and click Save.
Congrats! You now have the best and most up-to-date PHP version out there.
To check your current PHP version, all you need to do is go to the Hosting tab and check the left side panel for PHP version. If it’s anything less than 7.4, go ahead and update.
What’s New in PHP 7.4?
Since 2016, PHP7 has been releasing annual updates without fail. Each year they deliver on new features, additions, and the possibility to write cleaner code that makes the language more reliable and user-friendly for those who run it on their websites.
Let’s dig in and take a closer look at some of the changes that were made with the addition of PHP 7.4. For a full list check out their changelog here.
Let’s talk about code. When using a framework or libraries, its files have to be loaded and linked on every request. Preloading is when you can load frameworks and libraries into the OPCache. It allows for the server to load the PHP files and store them in memory during startup and have them available for any future requests. Talk about getting things going quick!
Preloading is run by a specific php.ini directive: opache.preload.This has the PHP script compiler and executes when the server starts-up. It can also be used to preload more files and choose to either include or compile them.
This is awesome, however, if the source of the preloaded files is ever changed, the server must be restarted. The preloaded files also remain cached in OPCache memory forever.
However, these preloaded files will continue to be available for any future requests in case you ever need to use them again.
Spread Operator in Array Expressions
Back when PHP 5.6 was released, PHP began supporting argument unpacking (spread operator) but now, with 7.4, we are able to use this feature with an array expression. Argument unpacking is a syntax for unpacking arrays and Traversables into argument lists. And, in order to do so, it only needs to be prepended by … (3 dots.) That’s it.
Let’s look at this example:
We can now expand an array from anywhere we want in another array, by simply using the Spread Operator syntax.
Here is a longer example:
Not only that, but you can also use it in a function. Check out this example:
In addition, you are now able to unpack arrays and generators that are returned by a function directly into a new array.
A code example would look like this:
And with PHP 7.4, it would print:
With this new array expression, spread operators should have way better performance over the 7.3 array_merge. This is because the spread operator is a language structure while array_merge is a function. Also because spread operator supports objects implementing traversable and array_merge only supports arrays.
Some important things to note, you can only use indexed arrays since string keys are not supported. If used, a recoverable error will be thrown on the screen, once a string key is found.
Another glorious benefit to 7.4 is the removal of the array_merge. Say goodbye to the dreaded index shift!
For example, let’s look at this long winded array merge below:
Another benefit of 7.4 is using the generator function. A generator function works just like a normal function, except instead of returning a value, a generator function yields as many values as it needs to.
Check out the example code below:
Now PHP 7.4 has a WeakReference class, which is not to be confused with the class WeakRed or the Weakref extension.
WeakReferences let the programmer recall a reference to an object. This is useful because it doesn’t prevent the object from being destroyed. They are helpful for implementing cache like structures.
Contravariant Parameters and Covariant Returns
Currently, PHP uses mostly invariant parameter types and return types. Meaning, if a method has a parameter or return type of X then the subtype parameter or return type must also be type X.
Now, with PHP 7.4 it proposes to allow covariant (ordered from specific to generic) and contravariant (reversing the order) on parameter and return types.
Here is an example of both:
Covariant return type example:
Contravariant parameter type example:
Typed Properties 2.0
Since PHP 5, type hints have been an available feature allowing you to specify the type of variable that is expected to be passed to a function or class. In the PHP 7.2 migrations, the addition of the object data type gave hope that more would be available in the future. The future is now.
In the new 7.4, PHP is able to support the following type list:
Note that the parent type can be used in classes and does not need to have a parent consistent with the parameter and return type.
Also, note that void and callable are not supported. Void was removed because it was not useful and had unclear semantics; Callable, because its behavior was context-dependent.
Let’s check out some more examples.
Here is a class, written for PHP 7.3:
In PHP 7.4, without sacrificing any type-safety, a class can now be written as simple as:
Here are some examples of all the types 7.4 now supports:
Arrow Functions 2.0
Anonymous functions in PHP tend to be wordy and lengthy, even when they are only performing simple operations. This is partially due to a large amount of syntactic boilerplate, and partially due to the need to manually import used variables.
This makes code that uses simple closures confusing to read and even harder to understand.
Let’s look at some code that you would use with PHP 7.3:
Now, here is the more concise syntax of PHP 7.4:
Therefore, arrow functions now have this simple form:
Below you can see an example of two functions $fn1 (7.3) and $fn2 (7.4) side by side. They have the same outcome but look different:
This will also work if the arrow functions are nested:
Here the outer function captures $z. Then, the inner function also captures $z from the outer function. With 7.4, the outer scope can become available in the inner function. This is something 7.3 wasn’t able to do.
The arrow function syntax allows for a variety of functions such as, variadics, default values, parameter and return types, as well as by-reference passing and returning. All while keeping a clean, readable look. Below are all the valid arrow functions now available:
One thing to note is that arrow functions have the lowest precedence. See the example:
There are many deprecations happening with the merge to 7.4. The following list is a short overview of the functions targeted for deprecation. You can find a more detailed explanation here:
- 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 directiv
Some important ones to note are the following two-step deprecations.
Changing the Precedence of a Concatenation Operator
Currently the precedence of ‘.’, ‘+’ and ‘-‘ operators are all equal. Any combination of these operators will simply be solved from left-to-right.
Let’s look at this code in PHP 7.3:
With PHP 7.4, ‘+’ and ‘-’ would take precedence over ‘.’ so the additions and subtractions would always be performed before the string. This would look like the following:
This two-step proposal aims to be less error-prone and more instinctive. PHP 7.4 currently is in the first stage with a deprecation notice of un-parenthesized expressions of ‘+’, ‘-’ and ‘.’ While waiting for the final vote/change happening in PHP 8.