PHP 7.4 went end of life at the end of November 2022 so I’ve been digging into what PHP 8 brings us, besides noticable performance improvements.

Here are the things I’m looking forward to right now.

Match Expression

Switch statements are powerful, and can be quite unwieldy if they get long enough. PHP 8 now brings us Match Expression to help clean up Switch.

In PHP 7 a switch statement that needed to return different text depending on input would look something like this.

switch( $input_text ){
	case 'one':
		$output_text = 'one';
	case 'two':
		$output_text = 'two';
	case 'three':
		$output_text = 'three';

PHP 8 allows us to simplify that to:

$output_text = match( $input_text ){
	'one' => 'one',
	'two' => 'two',
	'three' => 'three',

Far less typing and easier to read.

Better String Handling

There are three excellent new string functions to be used.

str_contains( $haystack, $needle ) returns true if the string ($needle) is contained inside the $haystack. The following will all evaluate to true.

// true because `thing` is somewhere in the string
str_contains( 'thing-one and thing-two', 'thing' );
str_contains( 'one-thing and another', 'thing' );

There are also two functions that allow you to evaluate if your string starts or ends with the search parameter.

// this is true
str_starts_with( 'thing-one thing-two', 'thing' );
// this is false
str_starts_with( 'one-thing and another', 'thing' );

// this is true
str_ends_with( 'thing-one thing-two', 'two' );
// this is false
str_ends_with( 'two-thing and another', 'two' );

Nullsafe Operator

We’ve all been in the spot where we have a nested object and we need to check at mutiple levels to see if we have a null value before we finally access the final item we want.

In PHP 7 our code would look something like this:

if ( $user->address ){
	echo $user->address->street;

Now with the nullsafe operator we could write the entire statement in one line.

echo $user?->address?->street;

If we have a null value at any level in that chain it will return null.

PHP 8 Speed Improvements

While PHP 8 doesn’t bring a massive speed boost like PHP 7 did, we still see some notable improvements. Kinsta showed that, compared to PHP 7.2, PHP 8.0 can handle 50% more requests.

I’ve upgraded 80+ sites I run and we’ve had a number of customers reach out asking what we did to speed the sites up. They had no idea anything was coming so this unprompted feedback from non-technical users shows that people notice when your site gets faster.

Can we use PHP 8 in WordPress?

Yeah the answer to this question for WordPress developers is, no you can’t start using PHP 8 right now for public projects like plugins/themes. According to statistics 56.4% of WordPress sites are still using PHP 7.4. If we broaden out our stats to look at any site running PHP 7, we have about 73% of active WordPress installs using PHP 7.

For my regular coding work, I could start using these PHP 8 features but only for plugins that I know are going to be on our install only. I actually can’t even do that until I resolve one or two legacy issues that have 4 of our sites running PHP 7.4 still.

We’ve only got a year until PHP 8.0 heads to end of life though, so maybe next year I can start using everything in PHP 8 without worrying about publicly released code.

Posted by Curtis McHale

Web developer specializing in membership and ecommerce sites. I like to ride my bicycle.