The Language which Powers 80% of the web doubles its speed in performance

“PHP 7 is a landmark achievement in the history of PHP and one of the most exciting moments in my career as a web developer. When the language that powers the majority of the web doubles in speed, it’s something to get excited about.” – Taylor Otwell, Laravel Founder

PHP 7 is released with numerous improvements and copious features. In this blog I have given a gist of the newly added features.

1. Return type declarations

We can now give return type declaration in PHP7

Illustration:

<?php
function sum($a, $b): float
{
return $a + $b;
}
// Note it returns a float
var_dump(sum(1, 2));
?>
Output: float(3)

2. Null coalesce operator

Null coalesce operator “??” is added which is similar to isset(). It returns the first operand if it exists and is not NULL otherwise it returns the second operand. One additional feature is that this operator can also used as a chain.

Illustration:

<?php
$usertype = $_GET[‘usertype’] ?? ‘Member’;
//equivalent to
$usertype = isset($_GET[‘usertype’]) ? $_GET[‘usertype’] : ‘Member’;
// chain
$usertype = $_GET[‘usertype’] ?? $_POST[‘usertype’] ?? ‘Member’;
?>

3. Scalar type declarations

The scalar type of declaration which can help us to use declare and use n number of variables during runtime.  It comes  in two flavours  coercive and strict

Illustration:

function sum(int …$ints)
{
return array_sum($ints);
}
var_dump(sum(8, ‘1’, 2.1));
Output: 11

4. Constant arrays    

We can define array constants now as below

Illustration:

<?php
define(‘USERTYPE’, [
‘Member’,
‘PortalAdmin’,
‘ChapterAdmin’
]);
echo USERTYPE[1];
?>
Output: PortalAdmin

5. Integer division

PHP 7 has introduced new function intdiv() to perform integer division

Illustration:

<?php
var_dump(intdiv(10, 3));
?>
Output: int(3)

6. Filtered

PHP 7 has a also concentrated to provide better security when unserializing objects on untrusted data which prevents code injections. Now developers can whitelist classes.

Illustration:

<?php
// converts all objects into incomplete classes
$data = unserialize($foo, [“classes_allowed” => false]);
// converts Class1 and Class2 as complete classes and all other as incomplete
$data = unserialize($foo, [“classes_allowed” => [“Class1″, “Class2”]]);
// Converts all objects into completed classes
$data = unserialize($foo, [“classes_allowed” => true]);
?>

Try this and get started with the new features, more flavours of PHP will be added in the next blog. 

LEAVE A REPLY

Please enter your comment!
Please enter your name here