It’s time - PHP 7 has arrived. The long awaited version promotes some significant speed improvements and enhances a lot of the sluggish ‘under-the-bonnet’ PHP codebase.

The new feature list for PHP 7 is pretty large so we’re going to touch base on the most important ones that we feel will aid development across any projects.

Major Speed Improvements

PHP 7 boosts a dramatic speed increase and a much lower memory footprint. Benchmarks have shown that PHP 7 is almost twice as fast as PHP 5.x releases. A lot of work has gone into improving PHP’s core interpreter and improving the overall performance of web applications.

Scalar and Return Type Declarations

It’s been fundamental to many programming languages for years and now we’ve finally got it in PHP too.

For both scalar and return type declarations we’ve got coercive and strict. You can now declare the type declaration as string, integer, float and boolean. Because it’s new to PHP 7, you want immediately get strict warnings if your scalar type doesn’t match but this can be enabled by placing a declare statement at the top of your code.

function sum(float $a, float $b): float
    return $a + $b;
function sum(float ...$floats): float
    return array_sum($floats);
sum([1.5, 2.5, 3.5]); // Returns 7.5
function sumArrays(array ...$arrays): array
    return array_map(function(array $array): int {
        return array_sum($array);
    }, $arrays);
sumArrays([1,1,1], [1,2,3], [2,2,2]); // Returns [3,6,6]

Null Coalesce Operator

Ever since I started programming in PHP I’ve hated the long-hand methods we have to put in place to get a relatively simple outcome. Take this example:

$pageNumber = (isset($_GET[‘page’]) ? $_GET[‘page’] : 1);

Now we can rewrite it with:

$pageNumber = $_GET[‘page’] ?? 1;

Let me Define an Array

That’s right, no longer are the define statements limited to primitive data types, you can now define an array - yay!

define('COLOURS', [
   'r' => 'Red',
   'g' => 'Green',
   'b' => 'Blue'
echo COLOURS['b']; // Blue

Namespace Improvements

It used to be a pain when you were working with Composer libraries and you had to write 10-15 use statements at the top of your page. With PHP 7 you can now write:

use MagnitudeCore{Blog, Article, User};
use function MagnitudeCoreHelper{methodOne, methodTwo, anotherMethod as methodThree};

Foreach Statement Handling

PHP 7 introduces quite a lot of changes for how foreach handles references, internal array pointer and modification of the array being iterated over. Take this simple example in which the internal array pointer will no longer be modified.

$array = [1, 2, 3];
foreach ($array as &$value) {
// In PHP 7, this will output int(0) int(0) and you guessed it… int(0)

There are some minor changes when iterating over a by-value data input. It’ll now iterate over a copy of the data you pass in, meaning any modifications to the array will not affect the values that are iterated over.

In contrast, when you’re iterating by-reference the execution block will keep track of changes to the array made during that iteration such that if you append / remove any elements before they’ve been iterated over, the execution will iterate over them accordingly, e.g.:

foreach ($array as &$value) {
   $array[] = 4;
// This will output: 1, 2, 3, 4 whilst in PHP 5.6 this would have only been: 1, 2, 3

The Last, Rather Confusing One: Division by Zero

It’s a funny topic and the guys behind PHP took a unique approach. Dividing by zero will now return 3 different responses depending on the scenario.

var_dump(1 / 0);
var_dump(0 / 0);
var_dump(0 % 0);
Warning: Division by zero in %s on line %d
Warning: Division by zero in %s on line %d
PHP Fatal error:  Uncaught DivisionByZeroError: Modulo by zero in %s line %d

Upgrading to PHP 7

On our last weekly roundup I mentioned PHP7-CC which is a great utility to check if your codebase throws up any errors that PHP 7 triggers.