PHP 7 Features

php-logo-Converted-01-3

Now that PHP 7 has been released for some time, lets start with some reasons on why to migrate our applications.

Speed & Memory

Not only PHP 7 has improved the use of memory but has multiplied considered times the amount of request that it supports. The speed is also been increased turning back PHP to a competitive coding language. “Cheers for all PHP developers”

Sooner or later we will rule the world!, Now seriously, here is a graphic of the improvement loading the default WordPress 4.1 in different PHP versions. you can have a look at Dual PHP + Apache for more notes.

PHP-7-performance-comparison1

Finally, typed variables declaration…

I was waiting for this. The main change is that now we will be able to add a return type to functions apart from string declarations. This will reserve the precise memory for each variable and will make the variables reduce usage of memory + increase the speed of processing.

<?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]));

If you are used to previous versions of PHP you will see that now there are types in the function declaration and the new type INT has been given birth in PHP.

Null coalescing operator (??)

Now you might be thinking, “nah, that’s not that usefull”. But it is, this kind of expressions are really useful is you intend to do bug-less code. Think that you should be asking constantly if the variable is defined and you should always send a value to functions.

Example:

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

// you can actually set a value depending on variables and in 1 single line

Spaceship operator

Now this is again a small improvement, because it was possible to be done before, but now we don’t even need a nested if. It’s doing it by library and so faster anyhow.

If we needed to do a binary search or just set a value over or lower, here is how you do it now:

<?php
// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 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

One expression to rule them all!

Others

There are some more features, you can find them all at Official PHP 7 new features

Conclusions

PHP is getting more and more usual, it’s the most used language after C++, It’s much more simpler than JAVA and can get along with a cheap machine. Lot’s of people start using it and lots of people have grown wealth with it. The time has come for PHP to be performant as other languages and with this new version it is a real revolution.

Beware all you developers that don’t know about types and start to be strict on you coding.