Top New PHP 7 Features Worth Mentioning
PHP 7 is here, but what about version 6? Well, the Unicode wasnât much successful and PHP had to leapfrog from version 5 to version 7. Nevertheless, everyone is excited about the new version. But before you take a leap, it is quite imperative that you take a deep look into the features of PHP 7. Here are some of the features of the same worth mentioning:
1. Fast speed: Increase in speed of operations is definitely the biggest talking point. It has been made possible by refactoring the PHP codebase that has removed some of the roadblocks resulting in significant increase in speed. It has been reported that PHP 7 is up to 2 times or even faster than PHP 5.6. Given that over 25% of the internet runs on Wordpress, an incredibly popular PHP based CMS, website loading speed are expected to increase all over.
2. Error handling: Error handling in PHP has been long considered weak. In case you have been even remotely involved with PHP development you would have surely come across fatal errors that invariably tend to result in stopping the script. Imagine throwing up a fatal error on a production server and the end user would be left wondering what to do next. It is worth mentioning here that a fatal error in PHP results in a blank screen and if that is shown to an end user instead of proper of error handling, it can result in enormous credibility hit.
Things are going to change a lot in PHP 7 in this regard. The latest version throws an exception instead of a blank screen in case of a fatal error; thereby giving the developers something to work on in case of anything untoward happens. However, it needs to be noted that warnings and errors continue to be handled in the same way as earlier and there is no change in this regard.
3. New operators: PHP 7 has also introduced some new operators such as spaceship operator and null coalesce operator. What are these and what are their advantages? Here is a short account of the same:
- Spaceship operator (< = >): Nice name, eh? Here is how this operator is supposed to work:
$compare = 10 <=> 5
10 < 5? return -1
10 = 5? return 0
10 > 5? return 1
As indicated above, it combines three operators, less than, greater than an equal. It performs all these operations individually and returns an answer as per the condition provided above. In case the value on the left is lesser as compared to that on right then return -1. If the two values are equal then return 0. And in case the value on the right is less than that on left it should return 1. This operation should be particularly helpful during sorting operation.
- Null Coalesce Operator: It is quite likely that you may have used the if-set-or operation during your past projects. Instead of writing the following:
- if (!empty($firstName)) $name = $firstName;
- else $name = âTestâ;
You can now write: $name = $firstName ?? âTestâ;
What this does that if the variable firstName is set and doesnât equal to null then its value will be assigned to name. In case it is null, the variable name will be assigned the value âTestâ.
4. Easy User-Land CSPRING: CSPRING refers to Cryptographically Secure PseudoRandom Number Generator. It is essentially generation of random data securely. While there is this function rand() in PHP 5, it isnât as secure as one would like it to be. Two new functions random_bytes() and random_int() have been introduced in PHP 7 that can help you to leverage random number generation function without having to compromise the entire system as in case of PHP 5.
5. Type Casting: PHP 7 has been long considered a weak typed language, meaning that it doesnât require you define types of variables and assigns them automatically instead. Type declarations have been introduced in PHP 7. From now on you can use scalar types such as strings, boolean numbers, floating points and integers. Here is an example hosting on Github:
<?php
/**
* Scalar type declarations
*/
//declare(strict_types=1);
function add(int $a, int $b) {
return $a + $b;
}
var_dump(add(1,2));
var_dump(add(â1â,â2"));
Strict: Turning on Strict types can result in fatal error in case return type does not match with that of the variable being returned.
The declaration for doing that follows:
declare(strict_types=1);
Do these changes in PHP 7 excite you? Well, they should do that in case you are involved into PHP Development. You may even refer to the PHP wiki if you wish to dig deeper into the newer features of the same.
Pushpendra Singh is an Freelancer PHP Developer working with F5 Buddy. You can contact her in order to hire a web developer to avail the highly functional web development and eCommerce solutions. He has several years of experience in the field of web development. He has successfully worked on various project and given on time delivery. Our team of developers bequeaths you with applications that are a testament to our commitment and unflappable focus on quality.















