WEB DEVELOPER SITE
PYTHON
 

PHP 7 Null Coalescing Operator


PHP 7 introduce null coalescing operator(??).

Mainly used to ternary operation in conjunction with isset() function.


Example

<?php
   // fetch the value of $_GET['password'] and returns 'not go'
   // if password is not go
   $password = $_GET['password'] ?? 'not go';
   print($password);
   print("
"); // Equivalent code using ternary operator $password = isset($_GET['password']) ? $_GET['password'] : 'not go'; print($password); print("
"); // Chaining ?? operation $password = $_GET['password'] ?? $_POST['password'] ?? 'not go'; print($password); ?>

Run example »

It gives following results





not go
not go
not go