As of December 1st, the newest version of PHP, PHP 7.1.0 is now available. After a huge update that took PHP from 5.6 straight to 7.0 increasing speeds considerably, PHP is now focusing on core language features that will help all of us write better code. In this article I’ll take a look at the major additions and features of PHP 7.1.0. You can also check out the official changelog.

Update: PHP 8.1 (official release) is now available to all Kinsta clients. PHP 7.1.0 is not supported at Kinsta. Please note that we support PHP versions 8.0, 8.1, 8.2 and 8.3.

Nullable Types

One of the most talked about additions will be nullable types which have been sorely missing. Variables can already return a value of some type or null and you can also set a function’s parameter to null be default. PHP 7.1 will support null as a definable return type for functions.

Let’s take a look at what this means. Here are some return types that you might find familiar from good ol’ 7.0.

function getNumber(): int  {
    return 6;
}

// Returns an integer so all is well

function getNumber(): int  {
    return 'six';
}

// Returns a string, will throw an error when called

Adding a question mark before the type declaration will allow null as a return value.

function getNumber(): ?int  {
    return null;
}

// Null is allowed, so all is well

The syntax is exactly the same for parameter typehinting. In the example below

function showColor(?string $color) {
    if ($color) {
        echo $color;
    }
}

showColor( '#ff9900' );
// Works just fine, a string has been given

showColor( null );
// Works fine, null is allowed

I love this feature because the more descriptive our code is, the better we can all work together. Type declarations are also great for weeding out any issues before they happen, a win-win all round.

php 7.1.0 changes

Iterable and Void Returns

While we’re on the subject of return types, there are two new ones: void. and iterable Void can be used for functions which don’t have a return value.

function perform_a_job() : void {
    return;
    // This is fine, it returns null
}

function perform_a_task() : void {
    // This is also fine, it returns null
}

function perform_another_task() : void {
    return true;
    // This will return an error since it is not void
}

This is another way to make our code self-documenting. Whether or not functions that don’t return anything should be used is up for debate, but we at least have a regulated mechanism to indicate this behaviour.

Iterable indicates a value which can be traversed, like an array. The added value of iterable is that we can use it to indicate an object that implements the iterator interface.

function fonc01(iterable $data) {
    foreach ($data as $key => $val) {
        //....
    }
}

fonc01(new SplFixedArray(5));
// Works kist fine, SplFixedArray implements Iterator
// See http://php.net/manual/en/class.splfixedarray.php

(Thanks to Pascal Martin for this example)

Multi-Catch Exception Handling

Up until now if a try block contained the possibility of multiple exceptions we needed to handle them separately, even if they used the same logic. This has been resolved in PHP 7.1 by allowing the catching of multiple exceptions in one go.

try {
    // some code
} catch (FirstException | SecondException $e) {
    // handle first and second exceptions
}

Keys Can Now Be Used in Lists

While list() looks like a function it is actually a language construct, just like array(). It is used to assign a list of variables in one go.

Until now it could only be used with numeric arrays starting from 0. From 7.1.0 on you can use keys with list(), or you can use its shorthand: []. Take a look at the examples below, courtesy of php.net.

$data = [
    ["id" => 1, "name" => 'Tom'],
    ["id" => 2, "name" => 'Fred'],
];

// list() style
list("id" => $id1, "name" => $name1) = $data[0];

// [] style
["id" => $id1, "name" => $name1] = $data[0];

// list() style
foreach ($data as list("id" => $id, "name" => $name)) {
    // logic here with $id and $name
}

// [] style
foreach ($data as ["id" => $id, "name" => $name]) {
    // logic here with $id and $name
}

More Negative String Offsets

PHP 7.1.0 adds wider support for negative string offsets. You can even start using negative offsets with [] in conjunction with strings. Take a look at how great this is:

$name = "Daniel";
echo "My name ends in an '$name[-1]'. Nice!";

Core functions such as strpos are getting this update which will make our code shorter and clearer.

Number Operators And Malformed Numbers

The fact that you can do 5 + "3" in PHP and it will evaluate to 8 is seen as a blessing to some, a curse to others. The problem is deepened when you realize that 5 + "three" evaluates to 5.

I think this is extremely sloppy and can lead to all sorts of problems down the line. PHP 7.1.0 attempts to bring some sanity to the situation by warning you when you have something malformed in there, like the second example. The first one will continue to work because PHP converts your string three to a number three. The second one will emit a notice or warning.

Other PHP 7.1.0 Small Additions

Aside from the myriad minor bug fixes there are some small features which should at least be mentioned:

  • SHA3 fixed mode algorithms added
  • Added is_iterable() function
  • Implement Closure::fromCallable
  • Add support for HTTP/2 Server Push
  • MCrypt has been deprecated and will be removed in the next version

Further Reading

I like the way PHP is heading – a more structured, more tightly typed language than before. Now that we’ve received a huge speed update with 7.0 it’s high time to focus on these aspects. If you’re interested in learning more about PHP, in general, I have this curated list of the best PHP tutorials and if you would like to learn more about 7.1.0 I can heartily recommend the following resources:

Also make sure to check out these performance benchmarks of PHP 7.1.0 vs PHP 7 and PHP 5. For source downloads of PHP 7.1.0 visit the PHP downloads page.

PHP 7.1, 7.2, and 7.3 for Kinsta Users

The latest version PHP 7.1, 7.2, and 7.3 is already available for all Kinsta users. It can easily be enabled from within the MyKinsta dashboard, under Tools, with a single click.

kinsta php 7.1
Kinsta PHP 7.1
Daniel Pataki

Hi, my name is Daniel, I'm the CTO here at Kinsta. You may know me from Smashing Magazine, WPMU Dev, Tuts+ and other WordPress/Development magazines. Aside from WordPress and PHP I spend most of my time around Node, React, GraphQL and other technologies in the Javascript space.

When not working on making the best hosting solution in the Universe I collect board games, play table football in the office, travel or play guitar and sing in a pretty bad band.