Back to Top

PHP 7 new Features at a Glance

php 7 at glance

In this article, We will discuss about the new features of PHP7.PHP7 will be officially released on November 2015. PHP7 will be a major update for PHP scripting language and it will bring significant performance improvements with new features.It will have improvement in some outdated features too. The release version will mainly focus on performance optimization to speed up execution of data.

Here I am going to explain some new features of PHP7

  1. Improved performance
  2. Consistent 64-bit support
  3. Many fatal errors will be consider as exception
  4. Removal of old and unsupported SAPIs and extensions
  5. The null coalescing operator (??)
  6. Combined comparison Operator (<=>)
  7. Scalar Type Declarations
  8. Return Type Declarations
  9. Anonymous Class Support
  10. UVS (Unified variable syntax)
  11. AST (Abstract Syntax Tree)
  12. error_clear_last () function
  13. Array in define () function
  1. Improved performance :

One of the major changes in PHP7 is a performance improvement. The performance improvement is quite an interesting feature because it is much faster and uses significantly less memory. so it will definitely bear much more traffic now without any additional costs.

  1. Consistent 64-bit support :

PHP 7 with full 64-bit support which is very impressive.It reduces the occupied size and optimize data structures.A small drawback new implementation – slightly excessive memory allocation.

  1. Many fatal errors will be consider as exception :

In New PHP 7, fatal errors will be significant as the exception so you can catch fatal errors and handle them in the future. Using this feature, fatal errors will be handled by a try / catch block and you will be able to continue to execute code without problems.

Example:

Currently, above code will generate error “Fatal error: Call to a member function method() on a non-object ” and a fatal error will stop the execution of next code too.

So, If you want to, you can continue to catch fatal error with exception:

  1. Removal of old and unsupported SAPIs and extensions :

Unfortunately, Numerous Server API(apache, ISAPI extensions) which no longer supported.

  1. The null coalescing operator (??) :

The Coalesce operator will be the very useful feature added into PHP7, we will certainly get used it very much easily.

Let’s understand it by checking old and new method:

The null coalescing operator will return value to his left if different than null while another case is the value is on the right side of the operator.

  1. Combined comparison Operator (<=>) :

This is some cool feature of PHP7.It is also known as spaceship operator.The new comparison Operator is looks like this <=>.

Here is a quick look at this feature with example:

As per above code, it will return -1 if the left side is larger than the right, return 0 if they are the same and return 1 if the right side is larger than the left. Isn’t it simple to use new operator?

  1. Scalar Type Declarations :

The next interesting feature is the Scalar Type Declarations. Scalar Type Declarations is a variable type declaration for scalar types int, float, string, and bool.

In both cases, Earlier PHP generates an error “Catchable fatal error: Argument 1 passed to function_scalar() must be an instance of int”.So you are not allowed to pass data type before the parameter into the function. However, It is newly added in PHP7.

Scalar Type Declarations is support two types:

1. coercive (flexible) :

This is default type and, in this case, $myinteger variable will receive the parameter type will be an integer for function, PHP will do an automatic conversion of the given value

2. STRICT :

STRICT type means if you use integer variable, it would contain integer value ​of variable and if float type in parameter, would store decimal values ​​and so on.

SYNTAX To enable strict mode:

This must be declared at the first line in the PHP files.when strict_types set to 0, PHP would not strictly check parameters and return value type.

So, with the STRICT Type would be allowed only integer type.

  1. Return Type Declarations :

Return Type Declarations should allow to indicate the return type of a function such as int, float, string and bool, array, callable.

  1. Anonymous Class Support :

PHP 7 introduced anonymous class which is previously supported by the most advanced language JAVA. Anonymous Class means declaration and the instance of the class take place in the same time.Also, it is not necessary to assign a name to it.

Let’s see the example to define and instantiate a class simultaneously:

  1. UVS (Unified variable syntax) :

The unified variable syntax will allow you to write some nice lines of code for which generally needed for temporary variables.From now onwards, you will be able to call the function assigned to the class attributes and also combine variables / static methods.

Some Examples:

  1. AST (Abstract Syntax Tree) :

Another wonderful feature of PHP 7 is the Abstract Syntax Tree (AST). Earlier,The PHP parser generates directly OpCodes whereas In PHP 7, the parser will work as an intermediate structure to generate the AST and parser will be decoupling compiler to reduce the amount of code and make it easier to understand and maintain easily.

  1. error_clear_last () function :

Added a new function error_clear_last () for clearing the last occurred error.

Example:

The error_get_last() function will improve the way to handle errors and you can easily get your last error in PHP.

  1. Array in define () function :

To assign an array values in define() function for constant was not possible before.It is possible to use array value in PHP7.

Let’s illustrate this with an example:

Earlier PHP7, we can not assign array value to constant so it would be very helpful features for us.

The php7​ features list is quite long.Certainly there are other more features too but not study in-depth so that’s it. I am really excited about the future of PHP 7. Do we really need those changes? Let me know if you like any of the new features in PHP 7.

Comments (3)

  1. Hello Bhumi,

    Awesome post, thank you very much. It features are very nice.

  2. Can’t wait to run my application on php7.
    Thanks for such a nice article !!

  3. Very nice article. I was waiting for array and object support in define function. Thanks.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to use MySQL SHOW Command

Posted on 6 years ago

Bhumi

How to use Template Parameter in Joomla

Posted on 6 years ago

Bhumi

How to use WebService in PHP

Posted on 3 years ago

Bhumi