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.

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.

Null coalescing operator

The null coalescing 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';

Spaceship operator

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

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.

Expectations

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

While the old API continues to be maintained for compatibility,
assert() is now a language construct, allowing the first
parameter to be an expression rather than just a string to be
evaluated or a boolean value to be tested.

<?phpini_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

Full details on this feature, including how to configure it in both
development and production environments, can be found in the
expectations section
of the assert() reference.

Group use declarations

Classes, functions and constants being imported from the same namespace
can now be grouped together in a single use statement.

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 finished yielding values.

<?php

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

return 3;})();

foreach ($gen as $val) { echo $val, PHP_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

Generators can now delegate to another generator,
Traversable object or array
automatically, without needing to write boilerplate in the outermost
generator by using the yield from construct.

Session options

These options have also been expanded to support
session.lazy_write, which is
on by default and causes PHP to only overwrite any session file if the
session data has changed, and read_and_close, which is
an option that can only be passed to session_start() to
indicate that the session data should be read and then the session should
immediately be closed unchanged.

The new preg_replace_callback_array() 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 to be polluted with lots of branching.

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.