David Jones

What to expect in PHP 7

PHP 7 is the next major version of PHP that is set to be released around November 2015. Lets take a look at some of the features that will be available.

Return Types

We can now define the data type of what ever is returned from a function, much like in C and Java.

If used this will prevent unintended return types.

For more information see the RFC.

Here is an example of the correct use.

function multiply (float $a, float $b): float {
    return $num1 + $num2;
}

multiply(1.5, 2);

Here is an example that will throw a fatal error.

function helloWorld(): array {
    return 'hello world';
}

helloWorld();

// Catchable fatal error: Return value of helloWorld() must be of the type array, string returned in %s on line %d

The Spaceship Operator

This is a new operator which looks like this <=>. It returns 0 if both operands are equal, 1 if the left operand is greater and -1 if the right operand is greater.

Some examples.

echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

For more information see the RFC.

Scalar Type Hints

This introduces four new types bool, float, int and string.

Here is an example.

declare(strict_types=1);
function multiply (float $a, float $b): float {
    return $num1 + $num2;
}

multiply(1.5, 2);

If you notice we called 'declare(strict_types=1)'. You need to do this at the top of each file that you want to use scalar type declaration.

For more information see the RFC.

Speed Improvements

The speed improvements come from using phpng as the base of PHP7. Some say it will make PHP7 almost twice as fast as PHP 5.6.