Features Of PHP 7 in Brief

Here is the Top 10 features of new and advanced PHP 7 . PHP is more powerful after realising PHP 7.

1)  Performance and CPU usage –

As previous versions of PHP was slow and facing performance problems. Now in PHP7 new caching system PHPNG(PHP New Generation). You can say this is the most advance development bench in PHP 7 . It increases memory efficiency and performance of PHP.  PHPNG is the alternative to PHP Cache(APC) and Zend Opcode. In PHP7 number of users requests are also increased tremendously.

You can check the performance graph as per the Zend framework of PHP7 on different PHP Frameworks and CMS.

What is HHVM in above graph ? –

HHVM (Hip Hop Virtual Machine) is developed  by Facebook. When Facebook decided to write their code in PHP on that time there is no PHP7 and number of request was increasing . Hence they have developed their own Hip Hop virtual Machine which converts code into  intermediate bytecode and translated in Machine language through (JIT) Just in Time Compiler. It was great success for them and number of requests handling gets increased because there is no converting and compilation in C++ and they have also removed zend engine to run PHP codes.

2) New Operators – 

a) Null coalescing operator (??) –

null coalescing operator is short form of isset to check if a variable or array node is set or not. In below example . In below example in $_GET[‘a’] has some value then it will set to $message variable if not then ‘not found’ string will be assign.

<?php  
//PHP 5 Style
$message = isset($_GET['a']) ? $_GET['a'] : 'not found';

//PHP7 style
$message = $_GET['a'] ?? 'not found';

?>

b) Spaceship operator  ( <=> ) –

The Spaceship Operator is a new comparison operator in php7. Which makes easy to compare two values . Spaceship operator return cases  –

# return 0 , If Both values are similar

# return -1 , If Left side value is smaller

# return 1 , If Right side value is smaller

It behaves same like strcmp function in PHP.

//Comparing Integers

 echo 1 <=> 1; //ouputs 0
 echo 3 <=> 4; //outputs -1
 echo 4 <=> 3; //outputs 1

 //String Comparison

 echo "x" <=> "x"; // 0
 echo "x" <=> "y"; //-1
 echo "y" <=> "x"; //1

 // Using with If section and string comparison 

$a = "jumbohost"; 
if($a <=> "Jumbohost")
{
   echo "Left Side Value is Smaller";
}

3) Scalar Type Declaration – 

This is the well demanding feature which was not in PHP 5 . PHP5 is explicit type definition in variable declaration. That means there is no type declaration required  in php5 and it automatically detects and  assign that much of memory to a variable. It also allows string and integer calculation without any warning or notice.

PHP 5 Code  –  There is data loss without any  warning.  in below example `Jumbohost` string data loss and there is no notification to developer.

<?php $a="1Jumbohost"; // integer due to the type juggling in PHP5
$b=20; //integer
function addition ($a, $b){
return $a + $b;
}
echo addition ($a, $b);  //  21

?>

PHP 7 Code –  PHP 7 Gives warning about this and developer can solve this immediately.  There is also a new directory declare is introduced . If warning is off and developer wants to more strict on this type of issues then use declare(strict_types=1) on very first line of the file and it will through fatal error. 

<?php $a="1Jumbohost"; // String
$b=20; //integer

function addition (int $a, int $b){
return $a + $b;
}
echo addition ($a, $b);  //  Warning: A non-numeric value encountered in /home/oleherbals/public_html/phptest/index.php on line 9

?>

Similarly we can define return type in a function also so that our function we be more accurate.

<?php declare(strict_types=1);
$a=1; //  integer
$b=2; // integer
function addition ($a, $b) : string  {
return $a+$b;
}
echo addition ($a, $b); 
// PHP Fatal error: Uncaught TypeError: Return value of addition() must be of the type string, integer returned
?>

Scalar type declaration is useful for neat and clean code writing also it can be better use in pup unit testings to test variable and functions.

4) Define Constants array –

This is also additional feature in PHP7 . We can assign array values while defining new constants using define keywords. Please see below example  –

define('NAMES', array('Man', 'Mohan', 'Singh'));

foreach(NAMES as $val)
{
    echo $val.' ';
}

5) Anonymous Class –  

In PHP5 there is anonymous function which called as closures but in PHP 7  you can define anonymous class also. In below code func2 returns object of anonymous class .   –

<?php 
class Outer
{
    private $prop = 1;
    protected $prop2 = 2;

    protected function func1()
    {
        return 3;
    }

    public function func2()
    {
        return new class($this->prop) extends Outer {
            private $prop3;

            public function __construct($prop)
            {
                $this->prop3 = $prop;
            }

            public function func3()
            {
                return $this->prop2 + $this->prop3 + $this->func1();
            }
        };
    }
}

echo (new Outer)->func2()->func3(); //6
?>

If you have any concern regarding above post please submit your comments .

Thank You .

Leave a Reply

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