New features in PHP 7.1

The PHP development team announced PHP 7.1.0 on 01 Dec 2016. This release is the first point release in the 7.x series. There are a few features – like the void return type – which have been introduced. Below are a few new selected features in PHP 7.1.

Void functions

A void return type has been added. Functions declared with void as their return type must either omit their return statement altogether, or use an empty return statement. Like in previous versions null is not a valid return value for a void function.

A void function can return in two ways.

We cannot return like this.

function should_return_nothing() : void {
    // Do something
    return null; // Fatal error: A void function must not return a value

This will throw the following error.

Fatal error:  A void function must not return a value

Note that void is only valid as a return type, not as a parameter type:

function foobar(void $foo) { 
    // Fatal error: void cannot be used as a parameter type

Nullable types

Type declarations for parameters and return values can now be marked as nullable by prefixing the type name with a question mark. This tells that besides the specified type, null can also be passed as an argument, or returned as a value, respectively. Nullable types are permitted anywhere type declarations are allowed but are subject to some inheritance rules.

function answer(): ?int  {
    return null; //ok
function answer(): ?int  {
    return 42; // ok
function answer(): ?int {
    return new stdclass(); 
    // Fatal error: Uncaught TypeError: Return value of 
    // answer() must be of the type integer or null
function say(?string $msg) {
    if ($msg) {
        echo $msg;
say('hello'); // ok -- prints hello
say(null); // ok -- does not print
say(); // error -- missing parameter
say(new stdclass); //error -- bad type

Note: Parameters with a nullable type do not have a default value. If omitted the value does not default to null and will result in an error:

function f(?callable $p) {}
f(); // invalid; function f does not have a default

PHP's existing semantics allow giving a null default value for a parameter to make it nullable and optional:

function foo_default(Bar $bar = null) {}
foo_default(new Bar); // valid
foo_default(null); // valid
foo_default(); // valid

Symmetric array destructuring

The shorthand array syntax [], introduced in PHP 5.4, may now be used to destructure arrays for assignments (including within foreach), as an alternative to the existing list() syntax, which is still supported.

Using the 'short array syntax' introduced in PHP 5.4 we can define an array as below.

 1, "b" => 2, "c" => 3];

Similar to the array() syntax for constructing arrays, PHP has had a syntax form for assigning to variables from array elements, know as "destructuring", since PHP 3.

With the new PHP 7.1 feature we can destructure and array like below.

 $a, "b" => $b, "c" => $c] = $array;

This syntax is more concise, and like the [] alternative to array(), this new syntax does not resemble a function call.

Importantly, this syntax for destructuring an array means there is now symmetry between array construction and destructuring, which should make it clearer what the function of the syntax is:

 $a, "b" => $b, "c" => $c) = array("a" => 1, "b" => 2, "c" => 3);
["a" => $a, "b" => $b, "c" => $c] = ["a" => 1, "b" => 2, "c" => 3];
list($a, $b) = array($b, $a);
[$a, $b] = [$b, $a];

Class Constant Visibility

Classes in PHP allow modifiers on properties and methods, but not constants. Class constant may be defined as public, private or protected and undeclared constants without any explicit visibility keyword are defined as public.

Support for negative string offsets has been added to the string manipulation functions accepting offsets, as well as to string indexing with [] or {}. In such cases, a negative offset is interpreted as being an offset from the end of the string.

In most PHP functions, providing a negative value as string offset means 'n positions counted backwards from the end of the string'. This mechanism is widely used but, unfortunately, these negative values are not supported everywhere. It is hard for most PHP developers to know whether a given string functions accepts negative values or not, and need to regularly refer to the documentation. Such inconsistency in the usage of string functions has given PHP a bad name.

An example is strrpos() accepting negative offsets, while strpos() does not. The same with substr_count() rejecting negative offset or length, when substr() accepts them.

One thought to “New features in PHP 7.1”

Comments are closed.