Skip to content
The Web Fosters: ideas into reality
Menu

php-7 new features, improvements, and benchmark

9 August 2016, by Nikhil Agrawal

Written in 2016. Versions and links mentioned here may be out of date.

php 7 features and benchmark

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

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 and 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:

declare(strict_types=1);
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', [
    'admin',
    'manager',
    'customer'
]);

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 {
    call_to_undefined_function();
} catch (Error $e) {
    echo "Error: ".$e->getMessage().PHP_EOL;
} catch (Exception $e) {
    echo "Exception: ".$e->getMessage().PHP_EOL;
}
//Type error

declare(strict_types=1);
try
{
    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.

php-7-indirect-variable-properties-method-order
List variable assign in serial order

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

  1. Magento benchmark with php 7:

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

  2. Drupal benchmark with php 7:

    Drupal runs 72% faster on php 7
    php7-benchmark-drupal

  3. WordPress:

    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.
    php7-benchmark-wordpress

  4. Laravel and Zend Framework2php7-benchmark-laravel-zend-framework2

source zend.com

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 .

Tags

Share this

Nikhil Agrawal

Nikhil is a Lead Web Developer working at The Web Fosters. A passionate coder and likes dealing with challenging algorithms. He has an expertise in web programming and is also a Zend Certified PHP Engineer.

2 comments

Comments are closed.

  • Unca Alby

    that left-to-right expression evaluation is going to mean a lot of code breakage.

    all of us, from time immemorial, are accustomed to multiplication having precedence over addition. admittedly, there is a plethora of sometimes obscure rules over that, nevertheless, I kinda think some basics ought to have preserved.

    I think we can easily deal with most of the rest of it when upgrading. but the precedence change is going to mean virtually every statement will need to be looked at closely and changed and tested.

    however, I am 100% behind converting errors to exceptions. PHP has been far too inconsistent with that behavior for far too long, where some errors throw exceptions, some display screen messages, and some simply fail quietly. converting all of that to throwing exceptions is clearly a step in the right direction.

  • D

    We are running all of our websites (mainly Laravel and WordPress) on PHP7 now. About 500+ domains. Runs perfectly! Just watch for mysql_query() and such, which have to be mysqli_*() or OO equivalent.