For the past seven years, PHP is the fourth most popular programming language in the world. After the release of PHP 5.6, it took 11 years for PHP developers to release new PHP version, however, the wait was worth it. PHP 7 comes with loads of new features and speed three times faster than PHP 5.6.
What are new features in PHP 7?
Integer Division
1 2 3 4 5 6 7 8 |
$a = 10.3; $b = 9.4; echo "Simple Div <br>"; $c = $a/$b; echo "$c <br>"; echo "Init Div <br>"; $c = intdiv($a, $b); echo "$c"; |
Scalar Type Declarations
1 2 3 4 5 6 |
// Coercive mode function sumOfInts(int ...$ints) { return array_sum($ints); } var_dump(sumOfInts(2, '3', 4.1)); // int(9) |
For example:
1 2 3 4 5 6 7 8 9 10 |
function div(float $x, float $y) { return $x / $y; } function sub(int $x, int $y) { return $x - $y; } var_dump(div(2, 3.5)); // float(0.57142857142857) var_dump(sub('2', 3)); // Fatal error: Uncaught TypeError: Argument 1 passed to sub() must be of the type integer, string given |
Comparison Operator:
PHP 7 comes with two new comparison operators one is spaceship or Combine Comparison Operator (<=>) and second is Null Coalesce Operator (??).
The benefits of Combine Comparison Operator is that you can use only to compare your values whether they are greater, less or equal. It returns 1 if the value compared is greater, 0 if they are equal and -1 if they are less.
For example:
1 2 3 4 5 6 7 8 |
// compares strings lexically var_dump('PHP' <=> 'Node'); // int(1) echo "<br>"; // compares numbers by size var_dump(305 <=> 456); // int(-1) echo "<br>"; // compares corresponding array elements with one-another var_dump(['a', 'b'] <=> ['b', 'c']); // int(0) |
The Null Coalesce Operator compare the value whether they are null or not. It is like isset(), which checks whether is it is empty or not. It returns the result of its first operand if it exists and not null, if it is null it checks the second operand. If you have defined many operand for comparison it will check all till it find a result which is not null.
For example:
1 2 3 4 5 |
$x = NULL; $y = 2; $z = 3; var_dump($x ?? $y ?? $z); // result will be int(2) echo "<br>"; |
Return Type
1 2 3 4 5 6 |
// Return Type is Float function sumOfInts(float ...$ints): float { return array_sum($ints); } var_dump(sumOfInts(2, '3', 4.1)); // float(9.1) |
Array Constant
1 |
define('ALLOWED_IMAGE_EXTENSIONS', ['jpg', 'jpeg', 'gif', 'png']); |
Generator Delegation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
function gen() { yield 1; yield 2; yield from gen2(); return yield from gen3(); } function gen2() { yield 3; return 4; } function gen3() { yield 5; return 6; } $gen = gen(); foreach ($gen as $val) { echo $val, PHP_EOL; } echo $gen->getReturn(); echo "<br>"; echo var_dump($gen); |
Deprecated Features in PHP7
PHP 4 Style Constructor
1 2 3 4 5 6 7 |
<?php class foo { function foo() { echo 'I am the constructor'; } } ?> |
Static calls to non-static methods
1 2 3 4 5 6 7 8 |
<?php class foo { function bar() { echo 'I am not static!'; } } foo::bar(); ?> |
1 |
<span class="pln">I am </span><span class="kwd">not</span> <span class="kwd">static</span><span class="pun">!</span> |