PHP Tutorial Tutorials - PHP Conditional Operator

PHP Conditional Operator

Conditional Operator is also known as Ternary Operator
There is only a single ternary operator,?:, which takes three values.

(expr1) ? (expr2) : (expr3)

Evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.

<?php
// if $_Get['page'] is NULL, return 'index',otherwise return the value of $_Get['page']
$page = isset($_Get['page']) ?$_Get['page']:'index';

// is identical to :
if (isset($_Get['page'])) {
    $page = $_Get['page'];
} else {
    $page = 'index';
}
?>

null coalescing

null coalescing ??,available as of PHP 7

<?php
// if $_Get['page'] is NULL, return 'index',otherwise return the value of $_Get['page']
$page = $_Get['page'] ?? 'index';

// is identical to :
if (isset($_Get['page'])) {
    $page = $_Get['page'];
} else {
    $page = 'index';
}
?>
Date:2019-10-01 02:16:37 From:www.Lautturi.com author:Lautturi