The All New PHP7

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. 

Vijayanand,
PHP Team Lead,
Mallow Technologies

2 Comments

  1. jasa SEO, Backlink, Blogwalking murah

    Hello I am so happy I found your site, I really found you by error, while I was browsing on Digg for something else, Anyways I am here now and would just like to say cheers for a fantastic post and a all round thrilling blog (I also love the theme/design), I don’t have time to browse it all at the minute but I have book-marked it and also added your RSS feeds, so when I have time I will be back to read a great deal more, Please do keep up the fantastic job.|

  2. Margarita

    Just desire to say your article is astonishing.

    The clarity in your post is just spectacular and
    i could assume you’re an expert on this subject. Well with your permission let
    me to grab your RSS feed to keep up to date with forthcoming
    post. Thanks a million and please carry on the rewarding work.

Leave a Comment

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