What's New What's New PHP 7.1.0
Since the 1st of December this year, the latest version PHP, PHP 7.1.0 is available. Following a major update which upgraded PHP up from 5.6 directly to 7.0 which increased speeds significantly, PHP is now focusing on features in the language that will help all of us write better code. In this article I'll take a look at the most significant additions and features that are included in PHP 7.1.0. Also, you can check the official Changelog.
- Nullable Types
- Iterable Returns and Void Returns
- Multi-Catch Exception Handling
- Keys Can Now Be Utilized in Lists
- More Negative String Offsets
- Operators of Numbers and Unformed Numbers
- PHP 7.1 and 7.2 for users
Nullable Types
One of the most talked about additions will be nullable kinds that have been sadly missing. Variables are already able to return the value of one kind or even null. You may also set a program's parameter to null be default. PHP 7.1 supports null as a return type for functions.
Let's look at the implications of this. There are a few return types which you may recognize from old 7.0.
function findNumber(): int
return 6;
// Returns an int and everything is fine.
call function callNumber() returns'six' int;
// Returns a string, is able to throw an error if it is called.
A question mark added before the declaration of the type allows null as a return value.
function, getNumber() int = return null
// null is permitted, and everything's fine
The syntax is exactly similar to that of typehinting parameters. The example below illustrates this.
function showColor(?string color) is echoing $color;
showColor( '#ff9900' );It works perfectly with a given string. shown color( null );// Works perfectly, null can be used
I love this feature because the more precise your code becomes, the better it is for us to work in tandem. Type declarations can also be useful for weeding out any issues ahead of time, which is a win-win for everyone.
Void and Iterable Returns
While we're on the subject of return types, there are two different types that are brand new: void. and iterable. Void is a good choice to perform functions that don't require any return values.
function perform_a_job() : void
return;
// This is fine, it returns null
function perform_a_task() : void
// This is also fine, it returns null
function perform_another_task() : void
return true;
// This will return an error since it is not void
It's another method to make our code self-documenting. The question of whether functions that don't return anything should be used is up for debate, however, we do can use a controlled mechanism to indicate this behaviour.
Iterable is a term used to describe a number that is able to be traversed like an array. The benefit of iterable is the possibility of using it to identify an object which implements the iterator interface.
function fonc01(iterable $data)
foreach ($data as $key => $val)
//....
fonc01(new SplFixedArray(5));
// Works kist fine, SplFixedArray implements Iterator
// See http://php.net/manual/en/class.splfixedarray.php
(Thanks to Pascal Martin for this example)
Do you want to know the ways we have increased visitors by 1000 per cent?
Join over 20,000 others to get our weekly newsletter with insider WordPress tricks!
Multi-Catch Exception Handling
For a long time, if a try block contained possible multiple exceptions, we needed to handle them separately regardless of whether they utilized the similar method of handling. This was solved in PHP 7.1 with the ability to allow the catching of multiple exceptions at once.
try
// some code
catch (FirstException SecondException $e) First and Second exceptions are handled
Keys can now be used in Lists
While the term list()
looks like a function it is actually an expression in the language, much similar to array()
. It's used to create a list of variables in the same go.
Until now it could only be used with numeric arrays beginning with the number 0. From 7.1.0 from 7.1.0 onwards, you are able to use keys that use list()
, or it is possible to use the shorthand: []
. Check out the following examples of php.net.
$data =["id" = 1, "name" => 'Tom'],["id" = 2 "name" = "Fred'][/ List() style
list() stylelist("id" ="id" = "name" =>"name1) is $data[0]
// [style["id" ="id1" = "name" => $name1"id" = $data[0]
// list() styleforeach ($data as list("id" = $id "name" => $name))
// logic here with $id and $name
// [] styleforeach ($data is ["id" => $id "name" and $name]) The logic here is $id, $name.
A Negative Offset for Strings
PHP 7.1.0 includes a wider range of support for negative offsets on strings. The possibility of using negative offsets by using the []
in conjunction with strings. Take a look at the amazing results:
$name = "Daniel";echo "My name ends in an "$name[-1]".. Nice! ";
Core functions such as strpos have been updated which will make our codes more concise and easy to understand.
Number Operators And Malformed Numbers
The ability to use 5+ "3"
in PHP and it will evaluate to 8 . This is viewed as a blessing to some, a curse to others. This problem gets more complicated when you consider that 5 plus "three"
evaluates to five.
It's extremely inefficient and could result in all sorts of trouble later. PHP 7.1.0 attempts to improve the issue by notifying you that there is something wrong in there, like the next illustration. The first will continue to work as PHP transforms the string you have entered into a three. A second option will issue an alert or notice.
Additional PHP 7.1.0 Small Additions
In addition to the numerous bugs that require minor fixes, There are a few minor features which should at least be noted
- SHA3 fixed mode algorithms added
- The function was added as iterable() function
- Implement Closure::fromCallable
- Support HTTP/2 Server Push
- MCrypt has been deprecated and will be removed the next version.
More Reading
Be sure to look over these performance tests for PHP 7.1.0 vs PHP 7 , and PHP 5. For source downloads of PHP 7.1.0 go to our PHP downloads page.
PHP 7.1, 7.2, and 7.3 for users
Reduce time, money and maximize site performance with:
- 24/7 help and support 24/7 support from WordPress experts in hosting, available 24/7.
- Cloudflare Enterprise integration.
- Global audience reach with 35 data centers worldwide.
- Optimization through the integrated Application Performance Monitoring.