What’s New in PHP 7.1?

by Ahmed Khan  December 2, 2016

Support for PHP 5.5 has just ended and PHP 7.0 has been released!

The combined impact of these two events have rocked PHP development world. In many instances, PHP developers have started to migrate their projects to PHP 7.0. To make sure that version 7 remains stable and perform as well as its predecessors, PHP 7.1 was released on December 1st, 2016.

PHP 7.1 Features

Let’s have a look at what’s new in PHP 7.1.

Nullable Types

With the introduction of Nullable Types, you can now return null from a function. This feature was already well supported in other languages including C# and Java. Nullable types are illustrated in the following example:

Void Function

The much awaited void function has been included inside PHP 7.1. You can now set void as the return type for your functions. For instance:

Symmetric Array Destructuring

In PHP 7.1, array syntax ([ ]) can be used to destructure arrays for assignment. This is an excellent alternate to list() syntax (which is also available). This structure can be used in a foreach statement for assignment. For instance:

Class Constant Visibility

You can now use access modifiers with a constant variable within the class to handle its visibility across the project. For instance:

Iterable Pseudo-type

PHP 7.1 has introduced a new pseudo-type that is similar to callable. It can be used in parameter and return type. It accepts either, arrays or objects, that implement Traversable interface. For instance:

Multi Catch Exception Handling

With the introduction of Multi Catch exception you can now catch more than one exceptions at a time in a single catch. For instance:

Support for Keys In list()

You can now specify keys while creating a list using list() function or using new shorthand syntax, i.e. ([ ]). For instance:

Support for Negative String Offsets

You now use negative offsets for string manipulation or can index it with ([ ]) or ({ }). Also, you can use simple variable parsing syntax inside string. For instance:

Convert Callables to Closures

PHP 7.1 will have a new static method Closure::fromCallable() within the Closure class to convert callables to Closure object. For instance:

Asynchronous Signal Handling

To reduce overhead using ticks, a new function pcntl_async_signals() has been introduced to enable asynchronous signal handling directly. For instance:

HTTP/2 server push support in ext/curl

You can now use HTTP/2 push using ext/curl. New constants CURLMOPT_PUSHFUNCTION,CURL_PUSH_OK and CURL_PUSH_DENY have been added in PHP 7.1 for curl_multi_setopt().

New Functions and Constants Introduced

Other than these new features, several new functions and constants have also been introduced in PHP 7.1. Some of new functions introduced are:

  • curl_multi_errno()
  • curl_share_errno()
  • curl_share_strerror()
  • session_create_id()
  • session_gc()

There are many other functions as well that can be found here.

Besides functions, some new constants have also been introduced. They are:

  • CURLMOPT_PUSHFUNCTION
  • CURL_PUSH_OK
  • CURL_PUSH_DENY
  • FILTER_FLAG_EMAIL_UNICODE
  • MT_RAND_PHP

I have listed only some of the major constants introduced. You can find other constants over here.

Migrating From 7.0.x to 7.1.x

Previously, we wrote a small guide on migrating from PHP 5.x.x to PHP 7.0.x. It was a bit complex to make the shift. However, if you have recently moved to PHP 7.0, then it will be easier for you to move to PHP 7.1. But before taking up the migration process, have a look at what features have been deprecated in PHP 7.1 from PHP 7.0:

  • ext/mcrypt has been deprecated in PHP 7.1 and will be removed from PHP 7.2
  • Eval option for mb_ereg_replace() and mb_eregi_replace() functions have been also deprecated.

Several functions have also changed in PHP 7.1 which can be found here. Complete migration guide can be found on PHP official site.

Related: How To Host PHP Websites On Amazon AWS Cloud

Conclusion:

PHP 7.1 is going to be a major change for PHP, helping it to compete in full force with other languages. In this article, we have discussed PHP 7.1 new features and functions and what to look for while migrating to PHP 7.1. If you have something to add to this, then feel free to leave a comment below.

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

About Ahmed Khan

Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works. You can email him at ahmed.khan@cloudways.com

Stay Connected:

You Might Also Like...