PHP 7 New Features

Link, Video

Major Feature

Scalar type hints

Type hints have been available in PHP for while now. Unfortunately they were restricted to classes, arrays and callables. As of PHP 7, the scalar types (integers, floating point numbers, booleans and strings) can also be used as type hints.
By default “coercive mode” is enabled. This restricts PHP from throwing a type error when the types don’t exactly match, but when a conversion is still possible.
If you enable “strict mode” (declare(strict_types=1);), a TypeError is thrown when the signatures don’t match.


Return type declarations

function add(int $a, int $b): int{ };

Whereas type hints ensure input consistency, return type declarations ensure output consistency. We use a colon before the opening curly brace of a function to hint the return type.
The same strictness rules apply as with the type hints: if “strict mode” is disabled, return values that can be converted to the preferred type are allowed. If you enable “strict mode” this code will throw a type error.

Return type declarations

The so-called “space ship operator” makes it easier to compare values. Instead of returning a typical true/false value, the space ship operator returns one of the follow values based on the result of the evaluation:

  • 0 when both values are equal
  • -1 when the left value is less than the right value
  • 1 if the left value is greater than the right value

The null coalesce operator

The null coalesce operator is a shorthand for checking if a value is set and not null within inline comparisons. Instead of doing the same old “isset” check over and over again, just use “??” to return the value if it is set (and not null) or an alternative value instead.



Anonymous classes

Anonymous classes are useful for simple one-off objects. With anonymous classes you can define a class and instantiate an object inline.
$foo = new class {
public function foo() {
return “bar”;


Array as constants Screen Shot 2017-11-07 at 2.40.58 PM define is set at runtime while const is set at compile time.

Unicode Code Point Escapes Syntax

Screen Shot 2017-11-07 at 2.46.24 PM


Uniform variable syntax

In PHP 7 the “uniform variable syntax” was introduced. This standard changes the way indirect access to array keys, properties and methods is evaluated. The PHP 7 interpretation enforces a strict left-to-right evaluation.uniform_variable_syntax



A big change in PHP 7 is the fact that errors are no longer raised the way they used to be raised. Errors now behave in a similar way as exceptions. They both inherit from the Throwable interface.

This means that errors can now be caught in a try/catch block. You can catch both exceptions and errors as Throwables, but you can also catch errors as Error objects. Code

There are event different kinds of errors we can catch:

This is also a backwards compatibility break because custom error handlers might not be triggered. If you have a parse error in an eval function, it will throw a ParseError instead of just returning false.Screen Shot 2017-11-07 at 3.12.13 PM.png

The Integer division function

Maybe not one the most important PHP 7 features, but still worth mentioning: the intdiv function returns the integer value of a division whereas regular divisions can result in a float being returned. Screen Shot 2017-11-07 at 3.18.40 PM



Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s