PHP people are hoping for release of PHP 7 in 2015. So 2015 is being considered as an important year for PHP as it’s been 11 years for release of PHP 5. As this is about be very be very major release of PHP, it has lot many things added, lot many things removed which was deprecated since long and many other things marked as deprecated as well.
I have started this article series to cover things which are new, deprecated and eliminated from PHP 7. In this article, we will be looking into new operators available in PHP 7.
PHP 7 ships with two new cool operators which are as below:
This is going to be very useful as its a shorthand for various if else conditions in your code. This operator is noted as <=> and also known as combined comparison operator.
If you want to compare $a with $b then you can use like $a <=> $b. This expression will return -1 if $a < $b, 0 if $a = $b, and 1 if $b > $a. In other words we can consider this new operator as a shortcut for below like of code.
Same as “Spaceship Operator“, this operator is also a shorthand for commonly used condition, and symbol for this operator is ??. Null Coalesce Operator can be used with one variable and it will check if any value set for that variable then it will return already set value else it will return default value which is defined. Let’s look into some code now:
Cool, Isn’t it? Yes I am feeling the same because this will reduce few characters in your code and more readable. In upcoming article we will see what are the new changes for Type Hinting we will receive in PHP7.
Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.
View all posts by: Avinash