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.

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

//coercive mode by default

functionsummation(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);

functionsummation(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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

functionsummation($val1,$val2):int

{

return$val1+$val2

}

var_dump(sum(1,2));//int(3)

var_dump(sum(1.1,2));//int(3)

//Strict mode:

functionsummation($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.

1

2

3

4

5

$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.

1

2

3

echo5<=>10;// -1

echo5<=>5;// 0

echo10<=>5;// 1

Array as a constant

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

1

2

3

4

5

6

7

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.

1

2

3

4

5

6

7

// Pre PHP 7 code

usesome\namespace\ClassA;

usesome\namespace\ClassB;

usesome\namespace\ClassC asC;

// PHP 7+ code

usesome\namespace\{ClassA,ClassB,ClassC asC};

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

1

2

3

4

5

6

7

try{

call_to_undefined_function();

}catch(Error$e){

echo"Error: ".$e->getMessage().PHP_EOL;

}catch(Exception$e){

echo"Exception: ".$e->getMessage().PHP_EOL;

}

1

2

3

4

5

6

7

8

9

10

11

12

//Type error

declare(strict_types=1);

try

{

functionsummation(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

1

2

3

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

Magento benchmark with php 7:

Runs upto 3x Magento transaction on same hardware.

Drupal benchmark with php 7:

Drupal runs 72% faster on php 7

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.

Laravel and 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.

Author

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

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.