Exciting new features in php 7

Exciting new features in php 7

Heard about PHP 7 but have very little idea about what it has in store for you? You have landed at the right place.

With the release of php 7, there are a set of excitingly new features and add-ons that would enhance your coding and overall performance of your website.

Lets dive into the details about what php 7 has in store for you:

  1. Lightening fast speed: A couple of benchmark tests have proved that php 7 is much much faster than php 5.6. This will help bring down the server response time since the processing would be faster and will positively impact your php application.
  2. Scalar Type Hints: In PHP there is ideally no need to mention the type of the variable. The PHP engine decides this at run-time depending on the value that is assigned to the variable. In the earlier versions of PHP like PHP 5 you could force passing objects of a particular type function declarations.

    function marksheet(Student $student){}

    Here we are forcing to pass an object of Type ‘Student’ to the marksheet function. If we pass any parameter other than this object it will produce an error.
    Prior to PHP 7 this declaration was only restricted to objects. But with PHP 7 this has been extended to using Scalar types like int, float, string and bool
    eg: We can now use

    function marksheet(float $marks){}
  3. Strict Mode: You can mention a line at the top of the file to enable strict mode on a particular file. By doing this if any of your code does not adhere to the policies of declaring the functions it will throw an error.
    To enable strict mode you need to place the following line at the top of the file ‘declare(strict_types=1);’
  4. Return type declaration: Similar to coercing the argument type you can also enforce the return type of the data returned from the function.

    return (string)($abc + $xyz);
  5. Error handling: In earlier versions of PHP whenever a fatal error occurred then it would directly lead to termination of the script. This was due to absence of proper error handling mechanism. With PHP 7 this problem is solved. These errors can be proper handled using the error handling mechanism since now both Errors and Exceptions implement the new Throwable class. Any error which is unhandled will continue to throw error and terminate the script

Hope this list helped you understand the new features of PHP 7.

Comments are closed.