Features Of the PHP 7

PHP 7 Features

PHP is one of the popular web development platforms. Recently PHP community has declared the new release of PHP 7.2 and already released PHP 7.1 with some bug fixing. Here we are going to learn some new Features Of the PHP 7.

PHP 7 introduced with some new advanced features to make programming more easy and high performance. Let’s have look New features of the PHP 7  that you have to know.

 

FEATURES OF THE PHP 7

1. Speed Improvement!

The developers for the PHP 7 has done a pretty commendable job here. Now your PHP codebase uses less memory and gives you, even more, performance than before. After it’s release, the internet was overloaded with benchmarks which were really promising. It is almost a 2x increase in server response times with PHP 7.

Benchmarks for PHP 7 consistently show speeds twice as fast as PHP 5.6 and many times even faster! Although these results are not guaranteed for your project, the benchmarks were tested against major projects, Drupal and WordPress, so these numbers don’t come from abstract performance tests.

2. Type Declarations

The type declaration is used to specify the type of variable. PHP sets this automatically and because of which, PHP is a weakly typed language. PHP does not need you to declare the data type. Because of which, you could do radical things with the variables, like adding Float into String without resulting in an error. Type declaration helps you get the expected results. Making sure that only certain procedures can occur. Which makes the code understandable.

In the previous PHP versions, we could use type hinting method. This method specifies the type of an argument declaration in a function. But the issue with this method was that you can only use it with a function declaration. Which limits to only two types, a class name, and an array.

 

     I.  Scalar Type Hints

With PHP 7 we now have added Scalar types.  Specifically: int, float, string, and bool.With PHP 7 we now have added Scalar types.  Specifically: int, float, string, and bool.

By adding scalar type hints and enabling strict requirements, it is hoped that more correct and self-documenting PHP programs can be written. It also gives you more control over your code and can make the code easier to read.

By default, scalar type-declarations are non-strict, which means they will attempt to change the original type to match the type specified by the type-declaration. In other words, if you pass a string that starts with a number into a function that requires a float, it will grab the number from the beginning and remove everything else. Passing a float into a function that requires an int will become int(1).

     II. Strict Example

     III. Non-Strict Example

     IV. Return Type Declarations

PHP 7 also supports Return Type Declarations which support all the same types of arguments. To specify the return type, we add a colon and then the type right before the opening curly bracket.

If we specify the return type of float, it will work exactly like it has been in the previous 2 examples since the type being returned was already a float. Adding the return type allows you to to be sure your function returns what is expected as well as making it easy to see upfront how the function works.

    V. Non-strict int

Without the strict type declaration on, if we specify the return type as int for the previous examples, it will work the same. With just the difference being, that return will be forged to an integer. Which means it will truncate the float value and only returns the integer.

     VI. Strict int

If we turn strict types on, we’ll get a Fatal error: Uncaught TypeError: Return a value of getSum() must be of the type integer, float returned. For this case, we’ll be casting our return value as an int. which then returns the truncated value.

3. Error Handling

The next new feature we will be discussing is the new Error Handling techniques implemented in PHP 7. Handling fatal errors was a nightmare in previous versions of PHP. If a fatal error occurs it just simply stops the script rather than invoking the error handler. On a production server, this returns a blank white screen to your users, which in the end causes your credibility to drop.

But PHP 7 allows an exception to be thrown when an error occurs, rather than stopping the whole script. This doesn’t mean that Fatal errors are gone from PHP 7. They still exist i.e., an uncaught exception will still be a fatal error in PHP 7. One more thing to highlight here is that other types of errors like warnings and notices are unchanged in PHP 7. And exceptions are only thrown by fatal and recoverable errors only.

I want to point out that other types of errors such as warnings and notices remain unchanged in PHP 7. Only fatal and recoverable errors throw exceptions.

     I. New Hierarchy

     II. Throwable Interface

If Throwable was defined in PHP 7 code, it would look like this

If you’ve worked with Exceptions at all, this interface should look familiar. Throwable specifies methods nearly identical to those of Exception. The only difference is that Throwable::getPrevious() can return any instance of Throwable instead of just an Exception.

Here’s what a simple catch-all block looks like:

4. New Operators

PHP 7 also brings us some new operators. The first one we’re going to explore is the spaceship operator.

     I. Spaceship Operator

Spaceship Operator, also known as the Combined Comparison Operator. It is put together using three of the previous operators, namely, “<”, “=” and “>”. It looks something like this:

<=>

What this operator is useful for is that it will compare the value on the left to the value on the right and returns 3 different values. See the example below for further understanding.

     II. Null Coalesce Operator

Another new operator, the Null Coalesce Operator, is effectively the fabled if-set-or. It will return the left operand if it is not NULL, otherwise, it will return the right. The important thing is that it will not raise a notice if the left operand is a non-existent variable.

$name = $firstName ?? “ABC”;

For example, name equals the variable firstName, double question marks, the string “ABC”.

If the variable firstName is set and is not null, it will assign that value to the variable name. Or else it will assign “ABC” the variable name.

Before PHP 7, you could write something like

if (!empty($firstName)) $name = $firstName;

else $name = “ABC”;

What makes this even more powerful, is that you can stack these! This operation will check each item from left to right and when it finds one that is not null it will use that value.

$name = $firstName ?? $username ?? $placeholder ?? “ABC”;

This operator looks explicitly for null or does not exist. It will pick up an empty string.

5. CSPRNG Functions

     What is CSPRNG?

The CSPRNG abbreviated as “Cryptographically Secure Pseudo-Random Number Generator” is easy to use API. It provides an easy and reliable way to generate secure random integers and bytes for use within cryptographic contexts. It can be used especially for generating random passwords or password salt.

2 new functions have been introduced in PHP 7 to use namely, “random_int” and “random_bytes”. Let’s analyze a bit more to see what these two brings to the table for the developers.

     I. Random Int

This function generates secure random integers. When using random_int you supply two arguments, that are min and max. Which tells the minimum and maximum numbers for the random integer.

random_int(1,20);

Would return a random number between 1 and 20, including the possibility of 1 and 20.

     II. Random Bytes

When using random_bytes, you supply a single argument, length, which is the length of the random string that should be returned in bytes. random_bytes then returns a string containing the requested number of cryptographically secure random bytes. If we combine this with something like bin2hex, we can get the hexadecimal representation.

These are bytes, not integers. For a random number or integer, you should use the random_int function.

6. Class Constant Visibility

In PHP, who have been wondering why couldn’t we specify the constant visibility in classes. Relax, this is now implemented. You can do it in classes and interfaces.

For those who don’t understand this is about yet let me explain this a bit. In classes you can specify the visibility of a method or a property like this:

7.  Void Return Type

PHP has started to be worried about its data types and this new version has considerably done a lot of work about it. We know we can return types now, besides we can precise nullable type. What if nothing at all? As you may know it, in most programming languages like C or Java there is the void type to say, there is nothing to return. Exactly! we can do the same thing now with this feature in PHP 7. All we’ll have to do is to use the returning type syntax and instead of using a data type keyword like int, float, etc will use void instead:

 

Above is the Features Of the PHP 7 which makes programming more easy and give the high performance for the web. There are many other features also introduced in PHP 7. Please write us your comment and ask us if you have any query.

 

Posted in PHP