PHP 7.0 was a significant release, but 7.1 built on it with a set of additions that made the language more expressive and easier to work with. If you are running PHP code in production, or maintaining a codebase that targets PHP 7.x, the changes in 7.1 are worth understanding. Several of them affect how you write type declarations, handle return values and work with arrays.
PHP 7.1 introduced nullable types, which let you declare that a parameter or return value can be either a specific type or null. Before this, you had to either leave the type hint off entirely or work around the limitation with additional checks.
The syntax uses a question mark prefix. For example, ?string means the value can be a string or null. This applies to both function parameters and return type declarations, giving you more precise control without sacrificing flexibility where null is a valid state.
Functions that do not return a value can now be declared with a void return type. This makes the intent of a function explicit in the signature rather than leaving it to documentation or convention. A function declared as void must not return a value, though it can use a bare return statement to exit early.
This is particularly useful in larger codebases where it is not always obvious whether a function is expected to return something. Declaring void removes that ambiguity at the language level.
PHP 7.1 added iterable as a pseudo-type for use in type declarations. A parameter or return value typed as iterable accepts either an array or any object that implements the Traversable interface. This is a common pattern in PHP, and having a single type hint to cover it removes the need to choose between array and Traversable or to omit the hint altogether.
Prior to PHP 7.1, class constants were always public. The new release added visibility modifiers, so you can now declare a constant as public, protected or private, the same way you would with a property or method. This brings constants in line with the rest of the class visibility model and is useful when a constant is an internal implementation detail that should not be accessible outside the class.
PHP 7.1 introduced a shorthand for array destructuring using square bracket syntax. Where you previously had to use list(), you can now write [$a, $b] = $array to assign array values to variables. The two approaches are equivalent, but the square bracket syntax is more concise and consistent with how arrays are defined elsewhere in the language.
This also works with foreach, so you can destructure each element of an array as you iterate over it without a separate assignment step inside the loop.
Exception handling became more concise in PHP 7.1 with the ability to catch multiple exception types in a single catch block. You separate the types with a pipe character, so catch (TypeError | InvalidArgumentException $e) handles either exception with the same block. Previously, you would need a separate catch clause for each type, even if the handling logic was identical.
String and array functions in PHP 7.1 gained support for negative offsets. A negative offset counts from the end of the string or array, so -1 refers to the last character or element. Functions including strpos(), substr() and str_split() were updated to accept negative values. This removes the need to calculate the length of a string before accessing characters near the end.
PHP 7.1 was a focused release. It did not overhaul the language, but the additions it brought, particularly nullable types, void returns and the iterable pseudo-type, addressed real gaps that developers had been working around for some time. If you are on a hosting plan that supports multiple PHP versions, you can switch between them from your control panel. Our guide to changing your PHP version in cPanel walks through the process.
If you are running PHP hosting with us and have questions about which version suits your application, get in touch and we can point you in the right direction.
Related articles you might find interesting.
Launch your website with our reliable cPanel hosting with unlimited bandwidth and expert support.
Get cPanel Hosting