What’s New in PHP 7.1.0

– Nullable Types
– Void Return Type
– Class Constant Visibility
– Multi Catch Exception Handling
– Support for keys in list()
– Support for negative string offsets
– Support for negative array offsets
– Support for the <=> Spaceship Operator
– Support for the Closure::call() Method
– Support for the Session Options
– Support for the CSPRNG Functions
– Support for the Expect Exception Annotations
– Support for the Iterable Pseudo-type
– Support for the Group Use Declarations
– Support for the Class Name Resolution As Scalar Expressions
– Support for the Unicode Codepoint Escape Syntax
– Support for the IntlChar Class
– Support for the Session Upload Progress
– Support for the Libsodium Extension
– Support for the Opcache Preloading
– Support for the Argon2 Password Hash Algorithm
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension
– Support for the libsodium Extension


As of December 1st, the newest version of PHP, PHP 7.1.0 is now available. After a huge update that took PHP from 5.6 straight to 7.0 increasing speeds considerably, PHP is now focusing on core language features that will help all of us write better code. In this article I’ll take a look at the major additions and features of PHP 7.1.0. You can also check out the official changelog.

Update: PHP 8.1 (official release) is now available to all Kinsta clients. PHP 7.1.0 is not supported at Kinsta. Please note that we support PHP versions 8.0 and 8.1.

Nullable Types

One of the most talked about additions will be nullable types which have been sorely missing. Variables can already return a value of some type or null and you can also set a function’s parameter to null be default. PHP 7.1 will support null as a definable return type for functions.

Let’s take a look at what this means. Here are some return types that you might find familiar from good ol’ 7.0.

function getNumber(): int  {
    return 6;
}

// Returns an integer so all is well

function getNumber(): int  {
    return 'six';
}

// Returns a string, will throw an error when called

Adding a question mark before the type declaration will allow null as a return value.

function getNumber(): ?int  {
    return null;
}

// Null is allowed, so all is well

The syntax is exactly the same for parameter typehinting. In the example below

function showColor(?string $color) {
    if ($color) {
        echo $color;
    }
}

showColor( '#ff9900' );
// Works just fine, a string has been given

showColor( null );
// Works fine, null is allowed

I love this feature because the more descriptive our code is, the better we can all work together. Type declarations are also great for weeding out any issues before they happen, a win-win all round.

php 7.1.0 changes

Iterable and Void Returns

While we’re on the subject of return types, there are two new ones: void. and iterable Void can be used for functions which don’t have a return value.

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
}

This is another way to make our code self-documenting. Whether or not functions that don’t return anything should be used is up for debate, but we at least have a regulated mechanism to indicate this behaviour.

Iterable indicates a value which can be traversed, like an array. The added value of iterable is that we can use it to indicate an object that 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)

Multi-Catch Exception Handling

Up until now if a try block contained the possibility of multiple exceptions we needed to handle them separately, even if they used the same logic. This has been resolved in PHP 7.1 by allowing the catching of multiple exceptions in one go.

try {
    // some code
} catch (FirstException | SecondException $e) {
    // handle first and second exceptions
}

Keys Can Now Be Used in Lists

While list() looks like a function it is actually a language construct, just like array(). It is used to assign a list of variables in one go.

Until now it could only be used with numeric arrays starting from 0. From 7.1.0 on you can use keys with list(), or you can use its shorthand: []. Take a look at the examples below, courtesy of php.net.

$data = [
    ["id" => 1, "name" => 'Tom'],
    ["id" => 2, "name" => 'Fred'],
];

// list() style
list("id" => $id1, "name" => $name1) = $data[0];

// [] style
["id" => $id1, "name" => $name1] = $data[0];

// list() style
foreach ($data as list("id" => $id, "name" => $name)) {
    // logic here with $id and $name
}

// [] style
foreach ($data as ["id" => $id, "name" => $name]) {
    // logic here with $id and $name
}

More Negative String Offsets

PHP 7.1.0 adds wider support for negative string offsets. You can even start using negative offsets with [] in conjunction with strings. Take a look at how great this is:

$name = "Daniel";
echo "My name ends in an '$name[-1]'. Nice!";

Core functions such as strpos are getting this update which will make our code shorter and clearer.

Number Operators And Malformed Numbers

The fact that you can do 5 + "3" in PHP and it will evaluate to 8 is seen as a blessing to some, a curse to others. The problem is deepened when you realize that 5 + "three" evaluates to 5.

I think this is extremely sloppy and can lead to all sorts of problems down the line. PHP 7.1.0 attempts to bring some sanity to the situation by warning you when you have something malformed in there, like the second example. The first one will continue to work because PHP converts your string three to a number three. The second one will emit a notice or warning.

Other PHP 7.1.0 Small Additions

Aside from the myriad minor bug fixes there are some small features which should at least be mentioned:

  • SHA3 fixed mode algorithms added
  • Added is_iterable() function
  • Implement Closure::fromCallable
  • Add support for HTTP/2 Server Push
  • MCrypt has been deprecated and will be removed in the next version

Further Reading

I like the way PHP is heading – a more structured, more tightly typed language than before. Now that we’ve received a huge speed update with 7.0 it’s high time to focus on these aspects. If you’re interested in learning more about PHP, in general, I have this curated list of the best PHP tutorials and if you would like to learn more about 7.1.0 I can heartily recommend the following resources:

Also make sure to check out these performance benchmarks of PHP 7.1.0 vs PHP 7 and PHP 5. For source downloads of PHP 7.1.0 visit the PHP downloads page.

PHP 7.1, 7.2, and 7.3 for Kinsta Users

The latest version PHP 7.1, 7.2, and 7.3 is already available for all Kinsta users. It can easily be enabled from within the MyKinsta dashboard, under Tools, with a single click.

kinsta php 7.1
Kinsta PHP 7.1

Get all your applications, databases and WordPress sites online and under one roof. Our feature-packed, high-performance cloud platform includes:

  • Easy setup and management in the MyKinsta dashboard
  • 24/7 expert support
  • The best Google Cloud Platform hardware and network, powered by Kubernetes for maximum scalability
  • An enterprise-level Cloudflare integration for speed and security
  • Global audience reach with up to 35 data centers and 275 PoPs worldwide

Get started with a free trial of our Application Hosting or Database Hosting. Explore our plans or talk to sales to find your best fit.





Source link

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?