What’s New in PHP 7.1.0

Updated on October 26, 2017

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.

PHP 7.1.0 is now focusing on core language features that will help us write better code. Click to Tweet

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 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 for Kinsta Users

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

kinsta php 7.1

Kinsta PHP 7.1

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
    Zachary Scott November 28, 2016 at 2:23 am

    I think that the push towards a more tightly typed PHP is kind of a departure from what makes it so good. Guess I am one of those people who thinks that 5 + “3” = 8 is a good thing :-D

  2. Gravatar for this comment's author
    Christoph Rumpel November 29, 2016 at 4:02 am

    Hey Daniel, thx for the article!

    What I was missing are some explanations for some of the features. Like the new list with keys. I understand the examples but I am not sure where this could be a benefit or how I can use that in production code.

  3. Gravatar for this comment's author
    mindpower August 26, 2017 at 12:58 pm

    More lipstick on a pig. What a stupid language. Get out while you still can!

Leave a Reply

Send this to a friend