New Operators under PHP7 version

  1. Null coalescing operator ( ?? ) : This operator is a replacement of ternary operator  in conjunction with isset(). It returns its first operand if it exists and is not NULL; otherwise it returns its second operand

If you are aware about ternary operator then well an good if not then I am giving you one example throw which you can understand and then I will try to explain how does Null coalescing operator work.

Example Of Ternary Operator:

Syntax: statement1 ? statement2 : statement3;
If statement1 is true then statement2 will be executed otherwise statement3 will be executed.

<?php
$a = 100;
echo $a ? $a : 'statement2';

Output : 100

<?php
$a = '';
echo $a ? 'statement1' : 'statement2';

Output : statement2
<?php
$a = '';
echo isset($a) ? 'statement1' : 'statement2'; Output : statement1

<?php
echo $a ? 'statement1' : 'statement2';

Output : Notice undefined variable a;
<?php
$a=null;
echo $a ? 'statement1' : 'statement2';
Output : statement2;



Example Of Null coalescing operator: Take the above example to understand the null coalescing operator.
Syntax: statement1 ?? statement2;
So in this case if statement1 exist and not null then return statement1 otherwise return statement2.

<?php
$a = 100;
echo $a ?? 'statement1';
Output :  100

<?php
$a = '';
echo $a ?? 'statement1';
Output :   in this case blank space will print  because of  " ??"  operator will check isset and not null

<?php
echo $a ?? 'statement1';
Output :  statement1
Note:  In case of ternary operator it will throw Notice error in the above example  but in case of  Null coalescing operator , it will not throw error because  of this operator will check isset as well as null at time.

<?php
$a = null;
echo $a ?? 'statement1';

Output :  statement1    ( Second operand executed because of $a is NULL)

So I have tried to explain purpose and use of Null coalescing operator  with the help of practical example .

2. Spaceship operator ( <=> ) : This operator works on two operands, if first operand is greater than second operand then this operator return 1, if first operand less than second operand then it return -1 and both operand equal then it return 0.

I have tried to explain the use of spaceship operator with the help of below example.

<?php
// Comparison with integer values
$a = 10;
$b = 5;

if (($a <=> $b)==1) // this comparison return 1 because of a is greater than b
echo "Variable a is greater than variable b";

// Output is : Variable a is greater than variable b

if (($b <=> $a)==-1) // this comparison return -1
echo "<br>Variable b is less than variable a";
// Output is : Variable b is less than variable a

$a = 5;
$b = 5;

if (($a <=> $b) == 0) // this comparison return 0
echo "<br>Variable a and variable b are equal";
// Output is : Variable a and variable b are equal

// comparison with float values
$a = 10.01;
$b = 10.001;

if (($a <=> $b)==1) // this comparison return 1
echo "<br><br>Variable a is greater than variable b";

// Output is : Variable a is greater than variable b

if (($b <=> $a)==-1) // this comparison return -1
echo "<br>Variable b is less than variable a";
// Output is : Variable b is less than variable a

$a = 10.00;
$b = 10.00;

if (($a <=> $b) == 0) // this comparison return 0
echo "<br>Variable a and variable b are equal";
// Output is : Variable a and variable b are equal

// String comparison

$a = "applered";
$b = "apple";

if (($a <=> $b)==1) // this comparison return 1
echo "<br><br>Variable a is greater than variable b";

// Output is : Variable a is greater than variable b

if (($b <=> $a)==-1) // this comparison return -1
echo "<br>Variable b is less than variable a";
// Output is : Variable b is less than variable a

$a = "apple";
$b = "apple";

if (($a <=> $b) == 0) // this comparison return 0
echo "<br>Variable a and variable b are equal";
// Output is : Variable a and variable b are equal

// Array comparison with integer values

$a = array(1,2,4,4); // Number of elements are 4 and the sum of elements is 11
$b = array(1,2,3,4); // Number of elements are 4 and the sum of elements is 10

if (($a <=> $b)==1) // this comparison return 1
echo "<br><br>Variable a is greater than variable b";

// Output is : Variable a is greater than variable b

if (($b <=> $a)==-1) // this comparison return -1
echo "<br>Variable b is less than variable a";
// Output is : Variable b is less than variable a

$a = array(1,2,3,4);
$b = array(1,2,3,4);

if (($a <=> $b) == 0) // this comparison return 0
echo "<br>Variable a and variable b are equal";
// Output is : Variable a and variable b are equal

// Array comparison with string values

$a = array("a","b","d"); // Number of elements are 3
$b = array("a","b","c"); // Number of elements are 3

if (($a <=> $b)==1) // this comparison return 1 because of both array have a and b which are same but the thirst element in $a has d which have the higher value than c of array $b
echo "<br><br>Variable a is greater than variable b";

// Output is : Variable a is greater than variable b

if (($b <=> $a)==-1) // this comparison return -1
echo "<br>Variable b is less than variable a";
// Output is : Variable b is less than variable a

$a = array("a","b","c");
$b = array("a","b","c");

if (($a <=> $b) == 0) // this comparison return 0
echo "<br>Variable a and variable b are equal";
// Output is : Variable a and variable b are equal

// Object comparison

$a = (object) ["a" => "b"];
$b = (object) ["a" => "a"];

if (($a <=> $b)==1) // this comparison return 1
echo "<br><br>Variable a is greater than variable b";

// Output is : Variable a is greater than variable b

if (($b <=> $a)==-1) // this comparison return -1
echo "<br>Variable b is less than variable a";
// Output is : Variable b is less than variable a

$a = (object) ["a" => "a"];
$b = (object) ["a" => "a"];

if (($a <=> $b) == 0) // this comparison return 0
echo "<br>Variable a and variable b are equal";
//Output is : Variable a and variable b are equal

If you want to check the above example then please copy and past to your PHP IDE.

Major changes in php7

  1.  Improvement In Exception Handling.
  2.  Few New Operators Added.
  3. Type Declaration is possible.
  4. Constant Arrays using define().
  5. Anonymous classes.
  6. Unicode codepoint escape syntax.
  7. Closure::call().
  8. Filtered unserialize().
  9. IntlChar.
  10. Group use declarations.
  11. Generator Return Expressions.
  12. Generator delegation.
  13. Integer division with intdiv().
  14. Session options.
  15. preg_replace_callback_array().
  16. CSPRNG Functions.
  17. Improvement In Speed.

PHP 7 new exception hierarchy

interface Throwable
    |- Exception implements Throwable
        |- ...
    |- Error implements Throwable
        |- TypeError extends Error
        |- ParseError extends Error
        |- ArithmeticError extends Error
            |- DivisionByZeroError extends ArithmeticError
        |- AssertionError extends Error

In the older versions PHP can not handle fatal error but using PHP7 we can handle fatal error rather than halting the execution.

Throwable interface has the following declared methods :

interface Throwable
{
    public function getMessage(): string;
    public function getCode(): int;
    public function getFile(): string;
    public function getLine(): int;
    public function getTrace(): array;
    public function getTraceAsString(): string;
    public function getPrevious(): Throwable;
    public function __toString(): string;
}

To catch Exception and Error object we can use throwable in try/catch block. That means  Throwable may be used in try/catch blocks to catch both Exception and Error objects but it would be good if you will use specific exception class according to the situation.

-> Explanation and use of  Error class.

Virtually all errors in PHP 5.x that were fatal errors or recoverable fatal errors now throw instances of Error in PHP 7.

Example: How to catch fatal error.

<?php
try{
    add();
}catch (Error $e) {
    echo $e->getMessage(). "\n";
}
?>

Above example will show the message  "Call to undefined function add()" for php7 and above version but in php5.x it will throw fatal error so that is big improvement of php7.

<?php
try{
  $obj = new People();//trying to create object of not existence class
}catch (Error $e) {
    echo $e->getMessage(). "\n";
}
?>

So in this example output will be "Class 'People' not found "

-> Explanation and use of  TypeError class.
There are three scenarios where a TypeError may be thrown.
1. The argument type being passed to a function does not match its corresponding declared parameter type.
2. Value being returned from a function does not match the declared function return type
3. An invalid number of arguments are passed to a built-in PHP function (strict mode only)


Example of first scenario :

<?php
function add(int $num1,int $num2)
{
    return $num1+$num2;
}

try{
    add(100,'b'); //Second parameter is string but it should be int.
}catch(TypeError $e){
    echo $e->getMessage();
    
}

If you will execute it then it will throw error like "Argument 2 passed to add() must be of the type integer, string given"

Example of 2nd scenario :

<?php
/**
 * If you want to use return type of the function then you need to
 * use declare (strict_types = 1); statement at the top of the file to  support return type
 * 
 */

declare (strict_types = 1); 
function add(int $num1,float $num2): int
{
    return $num1+$num2;
}

try{
    echo add(100,2.5);
}catch(TypeError $e){
    echo $e->getMessage();
    
}

If you try to execute this program then it will throw error like : Return value of add() must be of the type integer, float returned.
You have given return type of defined function add as an integer so if this function return other than integer then program will throw TypeError exception.

Now I am going to change the return type of above function from int to float then let see the result.

<?php
/**
 * If you want to use return type of the function then you need to
 * use declare (strict_types = 1); statement at the top of the file to support return type
 * 
 */
declare (strict_types = 1);
function add(int $num1,float $num2): float
{
    return $num1+$num2;
}

try{
    echo add(100,2.5);
}catch(TypeError $e){
    echo $e->getMessage();

}

Output of this program is : 102.5

Example of third scenario :

I am trying to explain this scenario with rand php in built function.As you know this function required only two parameters min and max. Now I am going to pass three parameters in this function without using strict_types.
<?php
try{
    echo rand(1,2,3);
}catch(TypeError $e){
    echo $e->getMessage();

}
If you try to run this program then it will throw error like : Warning: rand() expects exactly 2 parameters, 3 given in /opt/lampp/htdocs/testscript/php7/exception/typeerror.php on line 11.So this kind of error we can't show in front of the user.

So in the above example, exception  is not working so this program behave like php5.x version.So if you want to support TypeError exception then you need to add strict_types.

Use  strict_types.
<?php
declare (strict_types = 1);
try{
    echo rand(1,2,3);
}catch(TypeError $e){
    echo $e->getMessage();

}

If you try to run this program then it will show error like : rand() expects exactly 2 parameters, 3 given