PHP 8: The Complete Guide

On November 26, 2020, we entered into the new world of PHP 8. Every PHP update brings an entire host of brand new, major features. While not all of the updates are important to the majority of casual users, even other updates are too niche for the majority of seasoned WordPress developers.

But no matter what level of development you’re operating on, aspects of the new release, such as an increase in speed and breaking changes that might harm existing code, could negatively impact your daily workflow.

In this guide, we’ll walk through the most important changes in this new release of PHP 8. Let’s dive in.

In this guide

Why PHP Updates Happen

As an engine, PHP is changing constantly. New bits are always being modified or added to help optimize code and make PHP easier and simpler to work with.

WordPress plugin and theme creators, along with website developers, need to stay on top of PHP changes when they happen. By doing so, you’ll be a step ahead of the game and avoid pouring through pages of documentation after a few more updates have happened.

Each new version of PHP is intended to bring positive changes to the development environment. Unfortunately, there are times when certain tweaks can harm your existing code.

When incompatible, backward or outdated attributes clash with new PHP updates, it might end up breaking your entire site or causing areas of the site to stop working.

It’s a good idea to know what potential breaking changes are released so that you can address them directly within custom code. This also helps you make use of newer code that has more optimized expressions.

However, even if you haven’t touched a single piece of custom code in your career, when your WordPress dashboard adds a WordPress PHP Update Required notice for the updated version of PHP, you’ll have to decide if you’re going to upgrade or not.

Invariably, the answer to that question is nearly always yes. But before you do so, it’s smart to look over the major feature changes and ensure that there isn’t anything that might cause you problems. As the old saying goes, “It’s better to be safe than to be sorry.”

PHP 8

What’s New in the PHP 8 Release

This release of PHP is not, by any means, the biggest update that has come through. But there are still some exciting changes that we should look at.

In some ways, it’s a bit of a relief that PHP 8 is a relatively small update. This means that there are less breaking changes that could cause issues to your existing code.

In other words, the current PHP release gives you some great new features without the need to spend hours troubleshooting problems.

While there are some additional changes made in this version of PHP, this guide will cover the most relevant ones.

Just In Time (JIT) Compiler

The most exciting, and certainly biggest addition to the new version of PHP is the Just In Time, or JIT, compiler.

As you probably know, PHP is a language that’s interpreted. This means that it runs in real time rather than running at a pre-determined launch time after being compiled.

What JIT brings to the table for PHP is compiled code. With compiled code comes improved performance in certain situations.

If you’re like a lot of PHP developers, you’re probably working with web apps. Unfortunately, JIT won’t help much with web apps. But it marks a huge improvement when it comes to tasks such as:

  • Data analysis
  • 3D rendering
  • A.I.
  • Additional processes that take a long time to run

Although these aren’t necessarily the most common current PHP applications, forward-seeing developers are definitely branching out more with PHP.

The JIT compiler helps make PHP more flexible to do so.

In fact, JIT is almost guaranteed to open the horizons of PHP and bring in new developers that are interested in using it for new purposes.

For existing projects, it probably isn’t going to do much.

Before you implement JIT, ensure that you’ve tested it in an isolated environment to see if there is any improvement to your performance.

Attributes

The new version of PHP supports attributes. These are tiny pieces of metadata that you add to your code, such as parameters, classes and functions.

This means that you’re no longer required to use docblocks as a standard workaround. Although this sounds like a minor change, it actually makes life a lot easier for PHP developers.

You can now add any number of attributes to any piece of individual code, then import them with use statements.

You can even add parameters to attributes.

To add an attribute to your code, simply add two “less than” and two “greater than” signs surrounding the attribute.

It’s important to note that attributes aren’t backwards compatible. They’ll cause errors if they’re ported into an old version of PHP.

As you probably know, the standard workaround using docblock is fully functional. But it’s also always been quite clunky. With the new version of PHP, you can now directly add attributes.

Although it’s a small addition to PHP, it’s implications are huge.

New Match Expression

Switch statements were a more robust version of if statements. However, the new match expressions in PHP further improve on that concept.

This is shorter and stricter, which helps it better catch bugs. It also combines conditions and won’t require any break statement.

In these ways, the new match expressions are a marked improvement.

Switch still has the advantage over match because it has the ability to use multiple expressions. But besides that, consider doing some reading on match expressions.

So far they look like a superior replacement that you may want to utilize.

Union Types

One of they key parts of PHP are types: Assigning a variable as a boolean, integer, null and so forth.

Before this release of PHP, you were only allowed to assign variables with single types. PHP 8 allows you to assign two types or more, otherwise called a union type.

As an example, you’ll be able to assign a float and integer type and it can utilize either one. They’re specified with a line between the types, such as float|int.

Of course, you won’t be able to combine void with any other types because they’re mutually exclusive.

Redundant or duplicate types, such as float|float, also won’t work.

In older versions of PHP, you could use PHPDoc annotations to try to create a functional union type. Now, you can skip through that kind of workaround and simply assign your variables with multiple types.

Static Return and Mixed Types

As a developer, it’s common knowledge that PHP returns are used to return a function’s value. Before this release, it wasn’t an option to use the static type.

“Parent” and “self” were returns types that were valid, but “static” wasn’t. This issue was fully addressed in the new PHP version.

Although it’s not an incredibly common use case, if it does come up, you’ll definitely be glad you have access to it.

You’ll also find a new mixed type. It might more accurately be described as a pseudo-type because it represents every other type that exists in PHP.

It’s best used when you’re dealing with situations that require you to work with multiple different types.

Mixed will be your default if you don’t choose a specific type.

Often, it’s better to use the new union type rather than mixed. This is because it’s likely you’ll want to be more specific.

However, this one will still serve a purpose for you.

The reality is that neither of these are as big as the union type, but they definitely mark an improvement.

New PHP 8 Functions

The new version of PHP arrives with new functions that you can work with.

  • str_starts_with and str_ends_with – this will check if a string ends or begins with a specific line
  • fdiv – this will allow division that will consistently react when dividing by zero
  • str_contains – similar to str_starts_with and str_ends_with but will check a full string
  • get_debug_type – acts similar to gettype, but will return additional information, like class name
  • get_resource_id – this makes it easy to get an ID for a resource, such as a database connection
  • preg_last_error_msg – returns an error message that’s readable in plain language

Test some of these new functions to see how you can make the best use out of them.

Named Arguments

Some might love it and some might despise it. Either way, named arguments are in the new version of PHP.

Arguments let you pass data to a function. Named arguments let you do the same by passing the argument name rather than the order.

You’ll still be able to combine them with ordered arguments. But you’ll be able to skip over optional parameters if desired.

Practically speaking, this will mean cleaner code and few errors. After all, the more tight you code is, the better. It’s much easier to work with.

There’s a chance this could cause an issue with changing argument names and backwards compatibility, which could cause breaks in older PHP versions. But for a lot of developers and programmers, these named arguments are a welcomed change.

Breaking Changes in PHP 8

The new PHP release does come with a few breaking changes. This may cause errors if you upgrade or port back to old versions of PHP. Take a look at this list of breaking changes and see which ones will impact you.

  1. Engine warnings were reclassified so that they’ll show the appropriate level of severity, such as notices, warnings and fatal errors. Consider this a minor breaking change that won’t impact many projects.
  2. TypeError will now routinely show on internal functions. This impacts backward compatibility more than we may yet realize, but it’s a solid change nonetheless.
  3. Multiple changes were made to the error reporting levels that previously were set silent and ignored. In other words, new errors that you may not have been seeing before should now propagate.
  4. Sorting is more reliable and stable. Although PHP 7 made some big changes to sorting, the newest update does far less damage to existing code.
  5. Namespaced names now are a single token. They can’t have any whitespace inside. The impact of this change is small and simple to fix.
  6. In prior versions of PHP, methods that had a name matching the class were interpreted to be constructors. In the new version, you’ll have to use the __construct() method.

There are dozens of other small updates that will rarely cause any issues. Generally speaking, you won’t have a lot of issues with the current PHP update.

Upgrading Your PHP Version In WordPress

As a WordPress developer, you know how important it is to keep your version of PHP up-to-date. It’s alarming to know that a very significant portion of WordPress users are still running on PHP 5.6 which is a giant liability.

Even if you’ve been running PHP 7.4 and thought you were completely up-to-date, now is definitely the time to update to the newest PHP version.

Running your WordPress site on an older version of PHP is a major security risk. Outdated PHP versions have exploitable security flows that hackers regularly take advantage of.

The only way you can patch the existing pot holes is by keeping PHP completely up-to-date at all times.

You’ll also want to run a powerful WordPress security plugin to beef up your security protection.

Get the bonus content: A Guide to WordPress Security

Another reason to update your PHP is because of speed. With each update comes faster speeds and more optimized functions. Of course, the biggest noticeable difference was between PHP 5.6 and 7.4. But you’ll definitely notice an increase in speed with the newest update.

Compatibility is the final reason why it’s important to keep PHP updated in WordPress.

An outdated version of PHP can cause many different issues that can cause glitches or completely break a website.

Beyond there, there are themes and plugins that require a fully up-to-date PHP version in order to function.

Before running the update, make sure you have a WordPress backup plugin running in case there are any conflicts after the update runs.

Upgrading your version of PHP is quite simply. All you need to do is log into your cPanel, or the admin dashboard of the host you’re using, find the PHP Version Manager and tick the latest version. But before you run the update, check with your host to ensure that they are supporting the newest PHP update. Some may be running on older software.

For additional information on PHP and WordPress, check the official WordPress.org documentation on WordPress and PHP 8.

Wrapping Up: PHP 8 And the Future of PHP

While it’s not the monster update that PHP 7.0 – 7.4 were, PHP 8 still has a lot of things to offer that you can take advantage of.

From the JIT compiler to new attributes, match expression and other helpful functions for developers, it’s time to get familiar with this important new version of PHP.

Which parts of this update do you foresee using the most in your day-to-day workflow?

Spread the love

Posted by News Monkey

blank