PHP 7 – What’s new

PHP 7 is about 2 times faster than its predecessor (tested with popular applications such as Magento, Drupal, WordPress, SugarCRM), and uses less memory.

Modes of checking types of arguments

The default mode is coercive means allowing projection, for example string “8” will be replaced with the number of 8 for the function in which numeric type was declared. However, you can set the mode of strict, allowing only the exact type of argument. In such a case an error will be thrown, which you can handle in contrast to the older versions of PHP, where you would get a Fatal Error. Strict mode is declared for each file separately by typing at the opening of the php file: declare (strict_types = 1);

Coercive mode does not need to be declared since it is the default mode.

Become a Professionalist in Web Application Development! Enroll In The Course Of Laravel You will also learn how to create hybrid applications for smartphones

Declarations for scalar types

function sumOfInts(int ...$ints)
{
    return array_sum($ints);
}

try {  
var_dump(sumOfInts(2,'9',4.1,7,8,9));
} catch (Error $ex) { 
    echo $ex;
}

If the above example will do in strict mode, then the catch block will be executed, because we used the string type when we called the function. Without exception handler we would get a Fatal Error for an unhandled exception.

Declarations for return types

The return type is declared after the colon next to the parenthesis of the function definition.

function sum(int ...$numbers): int
{
    return array_sum($numbers);
}

try {
    $total = sum(3, 4, '6');
    echo $total;
} catch (Error $typeErr) {
    echo $typeErr;
}

If the return number is other than integer it will be converted to an integer and depending on the server configuration – a warning will be displayed about the wrong type.

Nested function calls, self-invoking function

function x()
{
    return function() {
        
        return function ($c){
            
            return 2+4+$c;
            
        };
    };
}
echo x()()(5);

The above properties results with the possibility of self-invoking functions:

(function ($a,$b) {
    echo  $a + 3 + $b;
})(2,1);

Anonymous classes

function d($x)
{
    var_dump( $x->sd() );
}

d( new class {

    public function sd()
    {
        return 'x';
    }
  
});

New operators

Comparison operator that returns one of three values: 1,0,-1:

var_dump(1 <=> 2); // int(-1)

var_dump('PHP7' <=> 'PHP7'); // int(0)

var_dump('PHP7' <=> 'PHP6'); // int(1)

var_dump([1,2,3] <=> [3,4,5]); // int(-1)

Null coalescing operator:

$page = $_GET['page'] ?? 1; // equivalent: $page = isset($_GET['page']) ? $_GET['page'] : 1;
$email = $_GET['email'] ?? $_GET['name'] ?? 'email@email.com';

Grouping use declarations

// older php versions
use Netprogs\Blog\Post;
use Netprogs\Blog\Author as User;
use Netprogs\Blog\Comment;

// PHP7
use Netprogs\Blog\{Post, Author as User, Comment};

Constant tables

define('ANIMALS', [
    'dog',
    'cat',
    'bird'
]);

echo ANIMALS[1];

Displaying Unicode characters in UTF-8

echo "\u{9999}"; // 香
echo "\u{1F49A}" // 💚

Safe filtering of unserialize function

$foo = serialize([1,2]);

// converts all objects into __PHP_Incomplete_Class object
$data = unserialize($foo, ["allowed_classes" => false]);

// converts all objects into __PHP_Incomplete_Class object except those of MyClass and MyClass2
$data = unserialize($foo, ["allowed_classes" => ["MyClass", "MyClass2"]]);

// default behaviour (same as omitting the second argument) that accepts all classes
$data = unserialize($foo, ["allowed_classes" => true]);

New functions in PHP 7

Among several new functions in PHP 7 there are available two cryptographically secure functions to generate random numbers random_int and strings random_bytes:

echo bin2hex(random_bytes(10)); // random characters
echo random_int(1,100);  // random number between 1-100

 

enroll in the course of Laravel framework >>>> or affiliate program
Share

Leave a Reply

Your email address will not be published. Required fields are marked *