The Getting Ready For PHP 7 Cheat Sheet

Updated on June 05, 2017

 

How Long Do We Have?

If you’re just hearing about this now, don’t worry, there are still a few months to go for sure. However, I would strongly recommend that you start looking into your projects, especially if they are non-WordPress based. According to the official timeline the final version will be out mid-October.

Up until now, hosts have been slow-ish to adopt new versions of PHP. It’s getting rarer, but I still see quite a few people with code issues because their server is running PHP 5.3 or lower which was replaced with 5.4.0 in early 2012. (Update: WordPress sites hosted at Kinsta are running on PHP 7.0 and 7.1)

I believe that this time around adoption rates will be significantly higher due to the huge speed increase the technology will bring. Hosts care about this because it will make them look good and will also allow them to cut back on resources, giving them more bang for their buck.

In a nutshell, you have about 2 months until the final version hits, I predict it will be a matter of days for the reputable hosts to make it available. For most systems this will be optional at first, but hosts that want to be on the forefront may make it the default for new servers very soon.

Will Anything Break?

Yes, PHP 7 will include a number of changes that will make existing code break. The good news is that most of these are removal of deprecated functions which you shouldn’t be using anyway. Here’s the list so far:

  • Script style tags and ASP style tags will not be available (<% and <script language='php'>)
  • All ereg_ functions will be removed but you can use preg_ functions instead
  • All mysql_ functions will be removed, you should use mysqli_ functions instead
  • The split function will be removed, you can use explode instead
  • <Multiple defaults in switches. Up until now the last defined default clause was used. From PHP 7, having multiple defaults will result in a fatal error.

That’s not a very long list and you probably shouldn’t be using these right now either, so the requirements to remove any breakages should be very low.

While it won’t produce a fatal error, the new uniform variable syntax can throw some nice notices at you. For example. You may have written something like this in the past: $object->$property['price']. In PHP 5.6 you could write it like this: $object->{$property['price']}. In PHP 7 you’ll need to switch things around and use this notation: {$object->$property}['price'].

PHP 7 New Features

A number of new features will be available in PHP 7 which will make our lives easier.

Combined Comparison Operator (Spaceship Operator)

A new operator type called the combined comparison operator has been added which is a useful little tool for comparing two integer values. The idea is that by writing $a $b we will end up with 0 if the two are equal, 1 if the left side is greater, -1 if the right side is greater.

Null Coalesce Operator

This is one of my favourite additions because it not only saves us from some cumbersome coding, but also sounds like something from a sci-fi movie dealing with black holes. The purpose of the null coalesce operator is to allow us to set values from user input without having to check if the value has been set. A quick example:


$orderby = $_GET['orderby'] ?? 'date';
// equivalent to: $orderby = isset($_GET['orderby']) ? $_GET['orderby'] : 'date';

New Type Hinting

For those of us who use OOP PHP, type hinting is a powerful tool. PHP 7 will include a bunch of new mechanisms including four new scalar types (float, int, bool, string) and return type hinting. I recommend reading the linked resource above for further info on how and why this is useful.

Anonymous Classes

Anonymous classes are for the OOP crows once again. They allow you to create unnamed classes when they don’t need to be documented or are only used once. A quick generalized example from the documentation:

var_dump(new class($i) {
    public function __construct($i) {
        $this->i = $i;
    }
});

Are Your Ready?

As you can see there isn’t a huge changelist, PHP 7 focussed more on speed increases, which is just fine by me. The net really will become a noticeably faster place overnight thanks to this. Keep an eye out on the official arrival date and make sure not to use anything which will break in PHP 7 and you should be on your way to full compatibility.

HHVM vs PHP 7

At Kinsta, we also support HHVM for all our plans, which provides 2-5x better performance than the official PHP FastCGI interpreter (FPM). We’ve written an in-depth article about the topic and you might be interested in this performance benchmark. HHVM vs PHP 7 – The Competition Gets Closer!

HHVM vs PHP 7

Further Resources

PHP 7 vs HHVM
It’s been in the works for quite some time and after a long wait PHP 7 was released on December 3, 2015. We tested the…
This article was written by Daniel Pataki

Daniel is the editor of the WordPress section on Smashing Magazine. He loves board games and going on runs with his dog.

Hand-picked related articles

  1. Gravatar for this comment's author
    andy October 3, 2015 at 3:07 am

    Are your ready?

    Yes my ready

    1. Gravatar for this comment's author
      Daniel Pataki December 3, 2015 at 8:19 pm

      I love how we haven’t changed this :D I’m glad your ready!

  2. Gravatar for this comment's author
    12centuries October 9, 2015 at 5:16 pm

    So you say we have a few months to go… but I have no idea when that will be because this article is lacking a date! I realize that putting dates on blog entries is not that popular now because heaven forbid that someone thinks a blog article wasn’t written this morning, but on TIME SENSITIVE articles, perhaps a date would be useful?! Consider your readers, please. (sorry for the rant, I’m just sick and tired of undated blog posts. It’s unprofessional.)

    1. Gravatar for this comment's author
      tomzur October 10, 2015 at 11:23 pm

      Hey, we have fixed this issue. You can find the publish date beneath the authors name. Sorry for the inconvenience, recently we made some design changes in the blog and forgot about this.
      Good luck with PHP7! Cheers, Tom from Kinsta

  3. Gravatar for this comment's author
    Leung WC March 31, 2016 at 7:55 pm

    The paragraph about $object->$property['price'] delivers wrong information. Please rewrite.

  4. Gravatar for this comment's author
    StuffieStephie February 6, 2017 at 9:54 am

    It’s also worth noting that PHP 7 has its own Error class. So if you have made an existing Error class of your own you’ll get the following error when you update.

    “Fatal error: Cannot declare class Error, because the name is already in use.”

Leave a Reply to Daniel Pataki Cancel reply

Use WordPress?

Join 20,000+ others who get our FREE weekly newsletter with WordPress tips on how to drive more traffic and revenue to your business!

You have Successfully Subscribed!

Send this to a friend