TutorialLab
New Feature In PHP 7

New Features In PHP 7 With Examples

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.

Thanks to all the developers who work day and night to develop PHP 7. It came up with so many new features and I will be listing some of the new features in this article.
Also Checkout PHP Beginner Course video tutorials

What are new features in PHP 7?

Integer Division

This is a new function introduced which handles the division of two numbers and returns the answer in integers. The function is intdiv(a,b) where a is the numerator and b is the denominator. It will take any data type as an input and return the answer only in an integer.
For Example:
$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

PHP 7 introduces Scalar Type declarations which comes in two categories. a weak type check and a strict type check. The difference between these is that, in weak you can enter any data type variable in function, which is on by default.
For Example :
// Coercive mode
function sumOfInts(int ...$ints)
{
    return array_sum($ints);
}
var_dump(sumOfInts(2, '3', 4.1)); // int(9)

But in strict type check, you can only add those variables whom data type are defined in the function. You can on it by adding declare(strict_types=1); at the top of your php code.

For example:

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:

// 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:

$x = NULL;
$y = 2;
$z = 3;
var_dump($x ?? $y ?? $z); // result will be int(2)
echo "<br>";

Return Type

PHP 7 now lets you define the return type of your function results. It can be string, int, float, bool, array, callable, self (methods only), parent (methods only), Closure, the name of a class, and the name of an interface. You have to define it while creating your function,
For example:
// 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

Now you can define an array constant in PHP7. Using the same define() function of PHP.
define('ALLOWED_IMAGE_EXTENSIONS', ['jpg', 'jpeg', 'gif', 'png']);

Generator Delegation

PHP 7 added a new functionality in Generator. It can now allow you to yield values from other generators too. They introduced a new keyword with yield from by which you can access another generator function.
For Example:
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

With the addition of new features, some old features have been removed or updated. Let’s have a look at the features that are deprecated in PHP 7.

PHP 4 Style Constructor

In php 7 they have removed those functions call, as constructors have the same name as the class have. But classes using _construct() method will be unaffected.
<?php
class foo {
    function foo() {
        echo 'I am the constructor';
    }
}
?>
Error
Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; foo has a deprecated constructor in example.php on line 3

Static calls to non-static methods

Calling to a non static function are now deprecated. Only those functions declared as static can be called.
<?php
class foo {
    function bar() {
        echo 'I am not static!';
    }
}
foo::bar();
?>
Error:
Deprecated: Non-static method foo::bar() should not be called statically in – on line 8
I am not static!

Salt Option

Salt option password_hash() is now deprecated from PHP 7. In order to prevent developers to generate their own unsecure salts, the function generates it’s own cryptographically secure salt.

capture_session_meta

Capture_session_meta has now been deprecated in PHP 7. Session metadata can now only be available through stream_get_meta_data function.
PHP 7 is eleven years in the making and is, hence, going to be the next big thing in programming languages. The features mentioned in this article the major improvements from previous versions of PHP. If I have missed any, then let me know in the comments box.

hassan

A Freelancer web developer, graphic designer and own this blog to spread knowledge.My hobby is computer problem's solving and i love to help other in this field.

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.