Migrating from PHP 5.6.x to PHP 7.0.x
PHP Manual

New features

Scalar type declarations

Scalar type declarations come in two flavours: coercive (default) and strict. The following types for parameters can now be enforced (either coercively or strictly): strings (string), integers (int), floating-point numbers (float), and booleans (bool). They augment the other types introduced in PHP 5: class names, interfaces, array and callable.

<?php
// Coercive mode
function sumOfInts(int ...$ints)
{
    return 
array_sum($ints);
}

var_dump(sumOfInts(2'3'4.1));

The above example will output:

int(9)

To enable strict mode, a single declare directive must be placed at the top of the file. This means that the strictness of typing for scalars is configured on a per-file basis. This directive not only affects the type declarations of parameters, but also a function's return type (see return type declarations, built-in PHP functions, and functions from loaded extensions.

Full documentation and examples of scalar type declarations can be found in the type declaration reference.

Return type declarations

PHP 7 adds support for return type declarations. Similarly to argument type declarations, return type declarations specify the type of the value that will be returned from a function. The same types are available for return type declarations as are available for argument type declarations.

<?php

function arraysSum(array ...$arrays): array
{
    return 
array_map(function(array $array): int {
        return 
array_sum($array);
    }, 
$arrays);
}

print_r(arraysSum([1,2,3], [4,5,6], [7,8,9]));

The above example will output:

Array
(
    [0] => 6
    [1] => 15
    [2] => 24
)

Full documentation and examples of return type declarations can be found in the return type declarations. reference.

Null coalesce operator

The null coalesce operator (??) has been added as syntactic sugar for the common case of needing to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.

<?php
// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';

// Coalesces can be chained: this will return the first
// defined value out of $_GET['user'], $_POST['user'], and
// 'nobody'.
$username $_GET['user'] ?? $_POST['user'] ?? 'nobody';
?>

Spaceship operator

The spaceship operator is used for comparing two expressions. It returns an integer less than, equal to, or greater than zero when $a is respectively less than, equal to, or greater than $b. Comparisons are performed according to PHP's usual type comparison rules.

<?php
// Integers
echo <=> 1// 0
echo <=> 2// -1
echo <=> 1// 1

// Floats
echo 1.5 <=> 1.5// 0
echo 1.5 <=> 2.5// -1
echo 2.5 <=> 1.5// 1
 
// Strings
echo "a" <=> "a"// 0
echo "a" <=> "b"// -1
echo "b" <=> "a"// 1
?>

Constant arrays using define()

Array constants can now be defined with define(). In PHP 5.6, they could only be defined with const.

<?php
define
('ANIMALS', [
    
'dog',
    
'cat',
    
'bird'
]);

echo 
ANIMALS[1]; // outputs "cat"
?>

Anonymous classes

Support for anonymous classes has been added via new class. These can be used in place of full class definitions for throwaway objects:

<?php
interface Logger {
    public function 
log(string $msg);
}

class 
Application {
    private 
$logger;

    public function 
getLogger(): Logger {
         return 
$this->logger;
    }

    public function 
setLogger(Logger $logger) {
         
$this->logger $logger;
    }
}

$app = new Application;
$app->setLogger(new class implements Logger {
    public function 
log(string $msg) {
        echo 
$msg;
    }
});

var_dump($app->getLogger());
?>

The above example will output:

object(class@anonymous)#2 (0) {
}

Full documentation can be found in the anonymous class reference.

Unicode codepoint escape syntax

This takes a Unicode codepoint in hexadecimal form, and outputs that codepoint in UTF-8 to a double-quoted string or a heredoc. Any valid codepoint is accepted, with leading 0's being optional.

echo "\u{aa}";
echo "\u{0000aa}";
echo "\u{9999}";

The above example will output:

ª
ª (same as before but with optional leading 0's)
香

Closure::call()

Closure::call() is a more performant, shorthand way of temporarily binding an object scope to a closure and invoking it.

<?php
class {private $x 1;}

// Pre PHP 7 code
$getXCB = function() {return $this->x;};
$getX $getXCB->bindTo(new A'A'); // intermediate closure
echo $getX();

// PHP 7+ code
$getX = function() {return $this->x;};
echo 
$getX->call(new A);

The above example will output:

1
1

Filtered unserialize()

This feature seeks to provide better security when unserializing objects on untrusted data. It prevents possible code injections by enabling the developer to whitelist classes that can be unserialized.

<?php

// converts all objects into __PHP_Incomplete_Class object
$data unserialize($foo, ["allowed_classes" => false]);

// converts all objects into __PHP_Incomplete_Class object except those of MyClass and MyClass2
$data unserialize($foo, ["allowed_classes" => ["MyClass""MyClass2"]);

// default behaviour (same as omitting the second argument) that accepts all classes
$data unserialize($foo, ["allowed_classes" => true]);

IntlChar

The new IntlChar class seeks to expose additional ICU functionality. The class itself defines a number of static methods and constants that can be used to manipulate unicode characters.

<?php

printf
('%x'IntlChar::CODEPOINT_MAX);
echo 
IntlChar::charName('@');
var_dump(IntlChar::ispunct('!'));

The above example will output:

10ffff
COMMERCIAL AT
bool(true)

In order to use this class, the Intl extension must be installed.

Expectations

Expectations are a backwards compatible enhancement to the older assert() function. They enable for zero-cost assertions in production code, and provide the ability to throw custom exceptions on error.

As with the old API, if the first argument is a string, then it will be evaluated. The second argument can either be a plain string (causing an AssertionError to be triggered), or a custom exception object containing an error message.

<?php

ini_set
('assert.exception'1);

class 
CustomError extends AssertionError {}

assert(false, new CustomError('Some error message'));

The above example will output:

Fatal error: Uncaught CustomError: Some error message

With this feature comes two PHP.ini settings (along with their default values):

zend.assertions has three values:

assert.exception means that an exception is thrown when an assertion fails. This is switched off by default to remain compatible with the old assert() function.

Group use Declarations

This gives the ability to group multiple use declarations according to the parent namespace. This seeks to remove code verbosity when importing multiple classes, functions, or constants that come under the same namespace.

<?php

// Pre PHP 7 code
use some\namespace\ClassA;
use 
some\namespace\ClassB;
use 
some\namespace\ClassC as C;

use function 
some\namespace\fn_a;
use function 
some\namespace\fn_b;
use function 
some\namespace\fn_c;

use const 
some\namespace\ConstA;
use const 
some\namespace\ConstB;
use const 
some\namespace\ConstC;

// PHP 7+ code
use some\namespace\{ClassAClassBClassC as C};
use function 
some\namespace\{fn_afn_bfn_c};
use const 
some\namespace\{ConstAConstBConstC};

Generator Return Expressions

This feature builds upon the generator functionality introduced into PHP 5.5. It enables for a return statement to be used within a generator to enable for a final expression to be returned (return by reference is not allowed). This value can be fetched using the new Generator::getReturn() method, which may only be used once the generator has finishing yielding values.

<?php

$gen 
= (function() {
    
yield 1;
    
yield 2;

    return 
3;
})();

foreach (
$gen as $val) {
    echo 
$valPHP_EOL;
}

echo 
$gen->getReturn(), PHP_EOL;

The above example will output:

1
2
3

Being able to explicitly return a final value from a generator is a handy ability to have. This is because it enables for a final value to be returned by a generator (from perhaps some form of coroutine computation) that can be specifically handled by the client code executing the generator. This is far simpler than forcing the client code to firstly check whether the final value has been yielded, and then if so, to handle that value specifically.

Generator Delegation

Generator delegation builds upon the ability of being able to return expressions from generators. It does this by using an new syntax of yield from <expr>, where <expr> can be any Traversable object or array. This <expr> will be advanced until no longer valid, and then execution will continue in the calling generator. This feature enables yield statements to be broken down into smaller operations, thereby promoting cleaner code that has greater reusability.

<?php

function gen()
{
    
yield 1;
    
yield 2;

    return 
yield from gen2();
}

function 
gen2()
{
    
yield 3;

    return 
4;
}

$gen gen();

foreach (
$gen as $val)
{
    echo 
$valPHP_EOL;
}

echo 
$gen->getReturn();

The above example will output:

1
2
3
4

Integer Division with intdiv()

The intdiv() function has been introduced to handle division where an integer is to be returned.

<?php

var_dump
(intdiv(103));

The above example will output:

int(3)

session_start() Options

This feature gives the ability to pass in an array of options to the session_start() function. This is used to set session-based php.ini options:

<?php

session_start
(['cache_limiter' => 'private']); // sets the session.cache_limiter option to private

This feature also introduces a new php.ini setting (session.lazy_write) that is, by default, set to true and means that session data is only rewritten if it changes.

preg_replace_callback_array() Function

This new function enables code to be written more cleanly when using the preg_replace_callback() function. Prior to PHP 7, callbacks that needed to be executed per regular expression required the callback function (second parameter of preg_replace_callback()) to be polluted with lots of branching (a hacky method at best).

Now, callbacks can be registered to each regular expression using an associative array, where the key is a regular expression and the value is a callback.

Function Signature:

string preg_replace_callback_array(array $regexesAndCallbacks, string $input);
<?php

$tokenStream 
= []; // [tokenName, lexeme] pairs

$input = <<<'end'
$a = 3; // variable initialisation
end;

// Pre PHP 7 code
preg_replace_callback(
    [
        
'~\$[a-z_][a-z\d_]*~i',
        
'~=~',
        
'~[\d]+~',
        
'~;~',
        
'~//.*~'
    
],
    function (
$match) use (&$tokenStream) {
        if (
strpos($match[0], '$') === 0) {
            
$tokenStream[] = ['T_VARIABLE'$match[0]];
        } elseif (
strpos($match[0], '=') === 0) {
            
$tokenStream[] = ['T_ASSIGN'$match[0]];
        } elseif (
ctype_digit($match[0])) {
            
$tokenStream[] = ['T_NUM'$match[0]];
        } elseif (
strpos($match[0], ';') === 0) {
            
$tokenStream[] = ['T_TERMINATE_STMT'$match[0]];
        } elseif (
strpos($match[0], '//') === 0) {
            
$tokenStream[] = ['T_COMMENT'$match[0]];
        }
    },
    
$input
);

// PHP 7+ code
preg_replace_callback_array(
    [
        
'~\$[a-z_][a-z\d_]*~i' => function ($match) use (&$tokenStream) {
            
$tokenStream[] = ['T_VARIABLE'$match[0]];
        },
        
'~=~' => function ($match) use (&$tokenStream) {
            
$tokenStream[] = ['T_ASSIGN'$match[0]];
        },
        
'~[\d]+~' => function ($match) use (&$tokenStream) {
            
$tokenStream[] = ['T_NUM'$match[0]];
        },
        
'~;~' => function ($match) use (&$tokenStream) {
            
$tokenStream[] = ['T_TERMINATE_STMT'$match[0]];
        },
        
'~//.*~' => function ($match) use (&$tokenStream) {
            
$tokenStream[] = ['T_COMMENT'$match[0]];
        }
    ],
    
$input
);

CSPRNG Functions

This feature introduces two new functions for generating cryptographically secure integers and strings. They expose simple APIs and are platform-independent.

Function Signatures:

string random_bytes(int length);
int random_int(int min, int max);

Both functions will emit an E_WARNING and return false if a source of sufficient randomness cannot be found.

list() can always unpack objects implementing ArrayAccess


Migrating from PHP 5.6.x to PHP 7.0.x
PHP Manual