Introduce New Features in PHP 7

Utpal Biswas
Oceanize Lab Geeks
Published in
3 min readJan 2, 2019

The PHP community is very excited to welcome this latest release. But it does’t mean that php has been stagnant at this time. They updated so many thing in this mean time but PHP 7 is totally new release with some excited features.

The SPEED is the amazing feature that PHP 7 bring in this release. The developers worked very hard to refactor the PHP code in order to reduce memory consumption and increase performance and they certainly succeeded. along with this there is some other features i am going to describe below.

Type Declaration :

As we know PHP is considered to be a weak typed language. In essence, this means that PHP does not require you to declare data types. Variables still have data types associated with them but you can do radical things like adding a string to an integer without resulting in an error. Type declarations can help you define what should occur so that you get the expected results. This can also make your code easier to read. We’ll look at some specific examples shortly below

Scalar Type Hints

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.

here is an example with non strict

function total(float $a, float $b) {
return $a + $b;
}

output

total(2.8, "3.2");
// string "3.2" changed to float(3.2) no notice
//returns float(6)

output with warning

total(2, "1 week"); 
// int(2) changed to float(2.0) and string “1 week” changed to float(1.0) but you will get a “Notice: A non well formed numeric value encountered”
//returns float(3)

If a type-declaration mismatch occurs, a “Fatal Error” is thrown with strict mood on. here is example

declare(strict_types=1);function total(float $a, float $b) {
return $a + $b;
}
total(2, "1 week");
// Fatal error: Uncaught TypeError: Argument 2 passed to total() must be of the type float, string given
total(2.5, 1);
// int(1) change to float(1.0)
//returns float(3.5)

as we see when we use strict mood “on” the warning became Error which is very helpful and structured way to understand and get the desired output from the codebase.

Return Type Declarations

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

function total(float $a, float $b) : float {}

New Operators :

PHP 7 also brings us some new operators. The first one we’re going to explore is the spaceship operator. The spaceship operator, or Combined Comparison Operator, is a nice addition to the language, complementing the greater-than and less-than operators. like

$compare = 2 <=> 1
2 < 1? return -1
2 = 1? return 0
2 > 1? return 1

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. like

$name = $firstName ??  "Guest";

Before 7.X it was like

if (!empty($firstName)) $name = $firstName;
else $name = "Guest";

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

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

This is fun is not it ??

If you’re ready to start playing around with PHP7 go throw all the changes in PHP 7. X

References:

New features in PHP 7. X

http://php.net/manual/en/migration70.new-features.php

Migrating from PHP 5.6.x to PHP 7.0.xhttp://php.net/manual/en/migration70.php

Deprecated features in PHP 7.0.x http://php.net/manual/en/migration70.deprecated.php

--

--