December 7, 2015 | In: Programovanie
php7 NULL coalesce operator ??
A bit weird and unintuitive coalesce behaviour, so watch out when using it!
$coalesce = true === false ?? 'no'; var_dump($coalesce); // false - huh?
so it seems that it works only for null :/
$coalesce = null == false ?? 'no'; var_dump($coalesce); // true
so the only valid and straight forward example comes from php.net:
$coalesce = $_GET['something'] ?? 'default'; var_dump($coalesce); // default
$username = !isset($_GET['something']) ?? 'wtf'; var_dump($username); // true
$_POST['second'] = 'chain'; $nc = $_GET['first'] ?? $_POST['second'] ?? $_REQUEST['third'] ?? 'wtf'; var_dump($nc); // chain
$nc = isset($_GET['something']) ?? 'wtf'; var_dump($nc); // false
Comments are closed.