What's New in PHP7

By hammad.usmani, 17 May, 2016

Currently, a new stable version of PHP 7.0.6 has been released containing the complete features for e.g. the 64-bit consistent support, many other new features like performance improvements and also a new version of the Zend Engine coming under the codename of PHP#NG (Next Generation)

PHP 7 latest version includes the following features:

Improved performance

One of the significant change & a new feature in PHP7 is the upgradation in performance. Now a days everyone want a fast and quick responsive website so no need to do extra wait because PHP7 has improved the performance and now it is much faster as compared to the older versions and plus consuming the less memory so it can definitely bear much more traffic at the peak times without any additional costs and down times etc. PHP has undergone significant improvements in performance over the years, making it a faster and more efficient language for web development

The following images clearly showing the comparison results:  

 
Here the execution time more than twice as compared to PHP 5.6 and 30% lower memory consumption.

Drupal 8 runs 72% faster with PHP 7.

Many fatal errors are now Exceptions

PHP as a language had a problem with the handling of fatal errors and we were unable to handle the error before it touches the front screen. And then one by one all error results in‚ set_error_handler()‚ and this lead to stop all script execution. It was the big problem for the Developers to handle the fatal errors and it just became a wish then, but now PHP7 has decreased these fatal errors significantly, not all but many of them. PHP7 will still throw the fatal errors same as the PHP version 5 for some conditions, such as running out of memory, and the faltering script execution etc.  
 

The new Null coalescing operator (??) 

The new operator named as null coalescing(??) always returns the left operand if it is not NULL, otherwise it will return the right one and the most important thing is that it will not create a notice and any error if the checking left variable is undefined etc.  
Example:

Old Syntax (before PHP7):  
$xyz = isset($var) ? $var : '0';

New Syntax (using Coalescing operator):  
$xyz = $var ?? '0';

We can also make a chain to get the first non-null.  
$config = $config ?? $this->config ?? static::$defaultConfig;

New Spaceship Operator (<==>)

It works under the official name of Combined Comparison Operator and its symbol is like (<==>)

It is also called the Three-Way-Comparison and it works as:

  • Returns 0 if both operands are equal.
  • Returns 1 if the left is Greater.
  • Returns 1- if the Right is Greater.

Return Type Declarations

Now in PHP7 the Return Type Declarations allows to indicate the return type of a function such as float, int, string, bool, array etc. 

Below is the function that returns an integer

function myFunc($a ,$b ){ return strval ($a + $b) ; }  
This code will return the String while

function myFunc(int $a , int $b ) :int { return strval ( $a + $b ); }  
this will return the int as the return type is defined.

Anonymous Class support

PHP 7 introduced anonymous class which is already supported by the most advanced language JAVA etc. Anonymous Class means declaration and the instantiation of the class in the same time. Also, it is not necessary to assign a name to it and we can pass arguments through to their constructors, extending other classes, implement interfaces, and use traits just like a normal class can.

AST (Abstract Syntax Tree)

Yes, Abstract Syntax Tree (AST), is another feature in PHP7. Earlier, the PHP parser generates directly Opcodes but now In PHP 7 the parser will be working as an intermediate structure to generate the AST and parser will be decoupling compiler to reduce the amount of code so it becomes easier to understand and maintain easily.

Scalar Type Declarations

It consists of declaring the types of function parameters and return values that can be of the types int, string, float and bool.

This allows the PHP runtime engine to check the types of values passed to parameter functions and returns from that are expected types to detect and minimize the eventual programming mistakes.


Scalar Type Declarations supports two types:

  • Coercive (flexible)

  • STRICT

Array in define () function

In older version before PHP7 we were not able to define the constant as an array, but now in PHP7 we can use array value as a constant.  
Example:  
define ('MYCONSTANT', array ('x', 'y', 'z'));

The Integer division function

The Integer Division function is not such a big feature in PHP7 but it is worthy to mention here to get aware, in a normal division like 10/3 we will get the result in float (Decimal Values) but while using the‚ “intdiv(10,3)‚ function it will return the Integer value.

Conclusion

PHP7 has robust the speed, better performance, compatibility and a comfort zone for the developers with the new operators and the catchable fatal errors as exceptions now. PHP is the technology that is being used widely in Web-Development and now the new version of the PHP is going to increase the stability and more market opportunities.

We AALOGICS as a WEB DEVELOPMENT FIRM providing the best services in the Web Development, Maintenance and the Support. We are expert in the CMS (Magento, Drupal) and the frameworks (YII, CodeIgniter) all using the PHP technologies.

PHP7