It has been a long time since the last major release of PHP, which is one of the most widely used languages in the world.
PHP 5.6 was released in 2005.About eleven years after that latest version of PHP was released in Dec 2015.As expected PHP 7.0
includes many improvements over the previous version.
The features provided by PHP 7.0 can be categorized into two main categories
Performance
PHPNG
If you have been using PHP since long then you will be happy to know that PHP 7 is nearly twice as fast as PHP 5.6.This is demonstrated by testing some major PHP projects such as WordPress and Drupal.
The main reason for improved performance of PHP 7 is the new version of Zend Engine ,Zend Engine 3. Zend Engine is an execution engine which is responsible for interpreting and executing PHP code. With Zend Engine 3 PHP applications run 2 X as fast as compared to the previous version of PHP. Zend engine 3 is also code named phpng.
Removal of Deprecated APIs
Many APIs were deprecated in PHP5.x.So most likely you might not be using these APIs.So to make PHP optimal these deprecated have been removed in PHP 7.Some of these are:
aolserver
apache
apache_hooks
caudium
Language Enhancements
Scalar type hints
As we know sometimes its good if a variable is assigned to a particular type to prevent unexpected errors from happening.
With the previous version of PHP we could specify the type of function arguments.
With Scalar type hints in PHP 7 we can also declare variables of scalar types.Scalar types are int, float, string, and bool.
function Add(float $a, float $b) { return $a + $b; }
Return Type Declarations
Return type declaration ensures that the function returns the correct type.For example a function expected to return int should not return string.
To enable type checking using Return type declaration we should enable strict type checking as:
declare(strict_types=1);
Now if we declare the function as:
function Add(int $a, int $b) : int { return $a + $b; }
and try to call
Add("a", "b");
we will get fatal error
Handling Fatal Errors
In PHP 5.6 when a fatal error occurs we can not catch it.So we have no other option to the default white screen being shown to the user.There was no support to catch Fatal errors.In PHP 7.0 we can catch the Fatal errors.We can catch Fatal errors using the EngineException class type.If the Fatal error is not caught then it will behave in a similar way to PHP 5.6 ,resulting Fatal error and blank screen shown to the user.
Spaceship operator
<=> is called the spaceship operator.This operator makes it easier to write comparison expressions.
$a<=>$b will return the following
1 if $a is greater than $b
0 if both are equal
-1 if $b is greater than $a
Null coalescing operator
This operator is denoted by ??.It makes it easier to check if variable is assigned a value.It is a good alternative to ternary ?x:y operator in many cases.Below are the two different ways of writing the same comparison expression in PHP 5.6 and PHP 7.0.You can see that the first expression is much easier to understand.
//PHP 7.0 ?? operator $loginId=$_GET['loginId']??'Empty'; //PHP 5.6 ternary operator $loginId=isset($_GET['loginId'])?$_GET['loginId']:'Empty';
Anonymous class
Anonymous class is a class which does not have a name.Anonymous class is a feature of other object oriented languages such as C# and Java.
You use anonymous class when you need a class only once.Anonymous class is defined as:
$obj = new class extends A{ public function Print() { } };
As can be seen there is a difference between how we instantiate object of anonymous class.In the case of anonymous class there is no name.Instead new operator is followed by the class definition.
Leave a Reply