php 7 new features

PHP 7 new features

1 – Speed Improvement

Now your PHP app gives you even more performance than before. After release, internet was overloaded with benchmarks which were really promising. It is almost a 2x increase inserver response times with PHP 7.

For further details on benchmarks click here.

2 – New Operators

Null coalescing operator

isset() The null coalescing operator (??) has been added

// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';

Spaceship operator

The spaceship operator is used for comparing two expressions.

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

3 – Anonymous classes

Support for anonymous classes has been added via new class

echo get_class(new class {});

4 – Scalar type declarations

Scalar type declarations come in two flavours: coercive (default) and strict

function sumOfInts(int ...$ints)
{
    return array_sum($ints);
}

5 – Return type declarations

function arraysSum(array ...$arrays): array
{
    return array_map(function(array $array): int {
        return array_sum($array);
    }, $arrays);
}