With the official release of PHP7 nearing ever closer, it seems like there isn’t a better time to buckle down and get a little bit more familiar with the latest PHP 7 features.

We have already covered setting up your development environment and ensuring your web applications are PHP 7 ready which you can read here: PHP 7 is Coming. Are Your Web Applications Ready?

Scalar Type Hints

Developers have had the option to use Class/Interface and array type hints since version 5 and 5.1 respectively. This has proven a very useful addition to the language and has been adopted by a large portion of the community. This is very useful when dealing with objects and arrays in PHP, but often developers have to deal with Scalar types such and int, double, bool and string.

Take this simple function as an example.

function getTotal($price, $quantity) {
    return $price * $quantity;

If you needed to ensure that $price is a double and $quantity is an integer in PHP5 you would have to use the built in is_double and is_int functions respectively:

function getTotal($price, $quantity) {
    if (!is_double($price) || !is_int($quantity)) {
        throw new Exception('Invalid data passed.');
    return $price * $quantity;

With PHP7 we can replace those with type hints and reduce the amount of code whilst increasing the readability, and also let the PHP interpreter throw more detailed and useful exceptions.

function getTotal(double $price, int $quantity) {
    return $price * $quantity;

Passing in any incorrect parameters should result in the following sort of error:

Fatal error: Uncaught TypeException: Argument 1 passed to getTotal() must be an instance of double, float given, called in /var/www/default/index.php on line 7 and defined in /var/www/default/index.php:3 Stack trace: #0 /var/www/default/index.php(7): getTotal(10.95, 2) #1 {main} thrown in /var/www/default/index.php on line 3

This simple change also provides the benefit of knowing at a glance what type of data needs to be passed to a function, without having to read the implementation.

Return Type Declarations

Return type declarations allow a developer to specify exactly what kind of data should be returned from a function call.

A particularly useful use case for this new feature is ensuring that DateTimes are always returned in the correct format from an object, for example an entity. It is quite common for an entity to be populated with a Date string instead of a DateTime object, a return type declaration should help to weed out any of these occurrences and reduce code like this in our projects.

$reliableDate = new DateTime($entity->getCreatedDate());

Instead we can add a return type declaration to our getter and be confident in knowing that we always get back a DateTime object, or a PHP error.

public function getCreatedDate() : DateTime {
    return $this->createdDate;

Null Coalesce Operator

The PHP language and community are mostly focussed on the development of web applications, because of this developers commonly need to process user data, If this data doesn't exist then generally we replace it with a default value. Surprisingly in previous versions of PHP this is quite a cumbersome and messy task, with a common solution being a ternary:

$data = isset($_GET['data']) ? $_GET['data'] : '';

Thankfully PHP7 has introduced the null coalesce operator, this acts in the same way as the ternary operator with an issue above but is much more readable. The null coalesce operator also makes it much easier to infer what is happening in our code. A simple example could look something like this:

$data = $_GET['data'] ?? '';

If we wanted to nest this to allow for a fallback value this can be easily achieved.

$data = $_GET['data'] ?? $_POST['data'] ?? '';

Along with the vastly improved speed of PHP7 these features should hopefully work towards helping PHP developers to write cleaner, more concise and more reliable code. If you want to learn a little bit more about the new PHP7 features you can read our blog post about the most important changes to PHP7.

Do you know of any new features in PHP7 that could be useful to developers? Tweet us @madebymagnitude, we'd love to hear from you!

Ignite your brand, utilise user-generated content no matter where you or your audience are ›