php-7 new features, improvements, and benchmark

Posted on Posted in cphalcon, Laravel, mamp, Phalcon, php

PHP 7.1 released on December 2015, still there are changes/fixes in progress with current version being PHP 7.0.13 (change log 7.0.13)

No doubt PHP development is easy, with PHP 7 it has become more developer friendly, 2x faster performance and 50% better memory consumption than PHP 5.6. In turn it allows to server more concurrent request without adding extra hardware.

In the article I will highlight some of the useful feature in php 7.0 , improvement in PHP 7.0 and benchmark of php 7.0 with Magento, Drupal, WordPress, Laravel, Zend Framework2, Phalcon, sugarCRM.
Scalar type declarations
Type declarations was known as type hints in PHP 5. Until PHP 7 type declaration was only for Class, interface, array and callable, but in php 7 type declaration for scalar type (bool, float, int, string) is also added.

Type declarations allow functions to have parameters (or arguments) of certain type at the time of calling. A mis-match in type generates an error(fatal error in php 5, TypeError exception in php 7)

Type declaration can be either coercive (default) or strict. If there is a mismatch in type then in case of coercive the value will be typecast into expected scalar value if possible, but in case of strict it will throw a TypeError exception.

//coercive mode by default

function summation(int $val1, int $val2)
    return $val1 + $val2

var_dump(summation(5,10)) // int(15)
var_dump(summation('5',10)) // int(15)

//Strict mode:

function summation(int $val1, int $val2)
    return $val1 + $val2

var_dump(sum(5, 10)); //int(15)
var_dump(summation('5',10)) // Fatal error: Uncaught TypeError: Argument 1 passed to summation() must be of the type integer, string given

Return type declarations
PHP 7 adds support to declare return type of a function. Similar to scalar type declaration it can be either coercive(default) or strict.

function summation($val1, $val2) : int
    return $val1 + $val2
var_dump(sum(1, 2)); //int(3)
var_dump(sum(1.1, 2)); //int(3)
//Strict mode:
function summation($val1, $val2) : int
    return $val1 + $val2

var_dump(sum(1, 2)); //int(3)
var_dump(sum(1.1, 2)); //Fatal error: Uncaught TypeError: Return value of summation() must be of the type integer, float returned

Null coalescing operator
The null coalescing operator (??) has been added for common case of using ternary operator with isset.

$name = $_GET['user'] ?? 'nobody';

//same as 

$name = isset($_GET['user']) ? $_GET['user'] : 'nobody';

Spaceship operator
Spaceship operator <=> is added to compare two values and return -1, 0, 1 in respective case of less than, equal, greater than.

echo 5 <=> 10; // -1
echo 5 <=> 5; // 0
echo 10 <=> 5; // 1

Array as a constant
In php 7 we can define array as a constant using define function.

define('USER_TYPE', [

echo USER_TYPE[0]; //output admin

Group use declarations
All classes in same namespace can be imported together in single use statement.

// Pre PHP 7 code
use some\namespace\ClassA;
use some\namespace\ClassB;
use some\namespace\ClassC as C;

// PHP 7+ code
use some\namespace\{ClassA, ClassB, ClassC as C};

Session options
session_start now accepts an array of options that override the session configuration directives normally set in php.ini

Errors converted to exceptions
In php 7 error are converted into exceptions, which means try/catch block can be used to catch error using Error exception class. This is backward compatible, so if Error exception is not handled then it will give a fatal error like in traditional PHP.

There are even different types of errors we can catch:

  • ArithmeticError
  • AssertionError
  • DivisionByZeroError
  • ParseError
  • TypeError
try {
} catch (Error $e) {
    echo "Error: ".$e->getMessage().PHP_EOL;
} catch (Exception $e) {
    echo "Exception: ".$e->getMessage().PHP_EOL;
}//Type error

    function summation(int $a, int $b):int {
        return $a + $b;
    echo summation('5', 10);
} catch (TypeError $e) {
    echo "Type error: ".$e->getMessage().PHP_EOL;

Uniform evaluation order
In opposed to previous version which has mix of variable evalation order, in php 7 variable, properties, and methods will now be evaluated strictly in left-to-right order.

List variable assign in serial order

code>list($a[], $a[], $a[]) = [1, 2, 3];

print_r($a); // array(0 => 1, 1 => 2, 2 => 3)

php-7 performance
PHP 7 is based on the PHPNG project (PHP Next-Gen), that was led by Zend to speed up PHP applications. The performance gains realized from PHP 7 are huge! PHP 7 uses new Zend Engine 3.0


Runs upto 3x Magento transaction on same hardware. php7-benchmark-magento



Drupal runs 72% faster on php 7



WordPress screams on php 7, one wordPress request on PHP 5.6 executes just under 100M CPU instructions, while with php 7 only executes 25M to do same job.



LARAVEL AND ZEND FRAMEWORK2php7-benchmark-laravel-zend-framework: (source


Doesn’t it looks really fast !!!

We are gearing up for PHP 7. We have installed php 7 on our test machine and started evaluating it.

Have you started the test ride 🙂 ? Please share your views .