PHP 5.6 VS PHP 7. What’s new in PHP 7 and what to consider when updating.

Most people who use PHP 5.6 wonder if a migration to PHP 7 would be beneficial or not for their applications and what this migration entails.

Before migrating to PHP 7, an analysis of the predefined functions used in the application is recommended. Although 99% of the functions predefined in php 5.6 will be found in PHP 7, there are some functions that in the version of PHP 5.6 appear as deprecated and in the version of PHP 7 have been removed.

For example, the eregi() function (with PHP version 5.3 it becomes deprecated and is completely removed in PHP version 7.0.0 and is replaced with the preg_match() function).

This analysis is very important to be able to replace the functions that are removed in the new version of PHP, a lack of this analysis can lead to the collapse of the application with its migration to the new version of PHP (risk of filling log files with warnings / errors / notices, blocking execution flows due to calling a non-existent function and other issues).

Most PHP 5.6 users fear that migrating to PHP 7 will make the development process more difficult in the first months due to the introduction of new features that are not known to programmers. Features like new operators, anonymous classes etc.). This argument is a very wrong one, because programming is constantly changing, and the stable higher versions of programming languages will always add to the applications and reduce the actual development time.

The period of accommodation of regular programmers with PHP 5.6 should be relatively short depending on their level of training because the syntax and the way the language works remain the same.

Migrating to higher stable versions should be one of the applications priorities, as switching from 5.6 to PHP 7 will be much easier than switching from PHP 4.0 to PHP 8 (the version to be released).

Migrating from PHP 5.6 to PHP 7 and later to PHP 8 will be a much smoother and more normal transition for an application regardless of its complexity. The risk of using PHP 5.6 can cap the team of programmers and bring a downside in terms of application evolution and performance.

Here are the main benefits of migrating to PHP 7:

  • the emergence of new operators
  • the ability to group packages using use
  • support for external code
  • higher performance
  • use of exact types

New operators are:

  • null coalescing (*)
  • spaceship operator()

* In terms of use in web applications null coalescing is the most used new operator and has the greatest impact.

In the following example we can see how to access a param query in PHP 5.6. example route:

https://www.exemple.com/client?id=2

The most common method of accessing the id parameter is this:

$id = isset($_GET[‘id]) ? $_GET[‘id] : null;

Although my recommendation would be this syntax:

$id = filter_input(FILTER_GET, ‘id’);

(a default value can be set if the id does not exist!)

In PHP 7 this syntax can be replaced by the null coalescing operator:

$id = $_GET[‘id] ?? null;

In this way we reduce the lines of code, and as a functionality this operator will check if the value is set and is different from null and will return it, otherwise it will move to the next value.

In this case you will check:

$id = isset($_GET[‘id]) && null !== GET[‘id] ? $_GET[‘id] : null;

* As a plus for those who are passionate about the most interesting things in PHP and good practices, the comparison using as a constant first element is an element of good practice (protects against mistakes)

For example, in the case of a typo that is replaced during the code development process a == or === with = then null = $id will generate an error, but $id = null will not lead to propagation a wrong flow!).

In terms of efficiency up to the version of PHP 7 (including PHP 7) the comparison with null is more efficient than using is_null! The spaceship operator is used to compare two expressions being a way to compare expressions in a much simpler and more compact syntax. In the case of a tie it will return 0. If the first operator is greater than the second it will return 1. If the first argument is less than the second it will return -1.

For example it can be used to compare data of type int, float, string:

10 < = > 10 -> 0
10 < = > 11 -> -1
10 < = > 9 -> 1

Another advantage is grouping using doors to reduce lines of code and create more compact code:

PHP 5.6:

use some\namespace\ClassA;
use some\namespace\ClassB;
use const some\namespace\ConstB;
use const some\namespace\ConstC;

PHP 7:

use some\namespace\{ClassA, ClassB};
use const some\namespace\{ConstB, ConstC};

On the external support side I would point out that most applications use external code (eg using the Composer to bring external packages). The external code needs to be updated, and over time most packages migrate to a higher version of PHP and reduce support for packages with an older version of PHP.

In order to have continuous support it is necessary to migrate to a higher version. Due to the use of the new Zend engine (migration to PHP next generation or PHPNG) the speed has been significantly improved due to the new engine uses better memory.

This improvement leads to serving a much larger number of users due to the fact that the threads will end their lifespan faster and will be able to serve new users due to the shorter execution time.

Using exact types for parameters eg:

function test(int $a) { 
  return $a 
}

Helps reduce the number of errors caused by incorrect data types. As a parallel, this improvement is similar to the coating offered by TypeScript for Javascript to reduce the number of errors within applications by moving them to the compilation.

Did you like what we wrote? Spread the word:

You might find helpful: