php 7 features and benchmark

php-7 new features, improvements, 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.

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.

Null coalescing operator

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

Spaceship operator

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

Array as a constant

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

Group use declarations

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

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

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

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 .

 

2 Comments

  • Unca Alby Reply

    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.

    2016-12-03 13:00:24
  • D Reply

    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.

    2016-12-02 13:08:51

Leave a comment

Your email address will not be published. Required fields are marked *