PHP 7 – New Operators

  • Avinash
  • 2
  • Jul 25, 2015
  • Tutorial

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 - New Operator

PHP 7 ships with two new cool operators which are as below:

  1. Spaceship Operator
  2. Null Coalesce Operator

Spaceship Operator

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.

// below lines performs the same thing in PHP7 // Old way ($a < $b) ? -1 : (($a > $b) ? 1 : 0) // New way $a <=> $b
    // below lines performs the same thing in PHP7
   
    // Old way
    ($a < $b) ? -1 : (($a > $b) ? 1 : 0)
   
    // New way
    $a <=> $b

null coalesce operator

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:

// below lines performs the same thing in PHP7 // Old Way $a = isset($b) ? $b : '0'; // New way $a = $b ?? '0'
    // below lines performs the same thing in PHP7
   
    // Old Way
    $a = isset($b) ? $b : '0';
   
    // New way
    $a = $b ?? '0'

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.

Related Posts

Written by Avinash

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:

  • Omid

    spaceship is same as , is’nt? :) :
    <?php
    $a = 'omid';
    $b = 'omidAli';

    echo strcmp($a , $b);

    //-3
    echo '’;

    $a = 12;
    $b = 1;

    echo strcmp($a , $b);
    //1
    echo ”;

    $a = 12;
    $b = 1;

    echo strcmp($b , $a);
    //-1
    echo ”;

    • http://www.xpertdeveloper.com/ Avinash

      Yes it is similar to

      strcmp
      strcmp

      but not fully similar. This operator is combination of various operator. Look at definition on php.net

      This RFC adds a new operator for combined comparison. Similar to strcmp() or version_compare() in behavior, but it can be used on all generic PHP values with the same semantics as <, =, >.