Learn How To Use Double Question Mark Operator In PHP

admin_img Posted By Bajarangi soft , Posted On 16-09-2020

Double question mark (??) operator mean in PHP will evaluates from left to right

Double question mark operator

List of Double question mark (??) operator mean in PHP

  • In PHP 7 has added a new operator double question mark (??) operator.
  • double question mark (??) operator known as Null Coalescing Operator.
  • It returns its first operand if it exists and is not NULL otherwise, it returns its second operand.
  • It evaluates from left to right.
  • Null Coalescing operator also can be used in a chain format.


Example(1)
<?php
     echo $x ?? 6 ??  32; //$a is not set
?>

output : 6

Example(2)
<?php

$b = 54;
echo $a ?? $b ?? 5;//$a is not set
?>

output : 54

Example(3)
<?
$foo = $bar ?? 'something';
$foo = isset($bar) ? $bar : 'something';

echo $foo;
?>
output : something


Complete Code to Double question mark (??) operator  :
<!DOCTYPE html>
<html>
<head>
    <title>double question mark (??) operator mean in PHP</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <br>
    <br>
    <div class="text-center">
        <h1>Double question mark (??) operator mean in PHP</h1>
    </div>
    <br>
    <br>
    <div class="well">

        <?php
        //$a is not set
        echo $x ?? 6 ??  32;
        
        echo "<br>";
        echo "<br>";
        //$a is not set
        $b = 54;
        echo $a ?? $b ?? 5;

        echo "<br>";
        echo "<br>";

        $foo = $bar ?? 'something';
        $foo = isset($bar) ? $bar : 'something';

        echo $foo;
        ?>
    </div>
    <br>
</div>
</body>
</html>

Related Post