Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /data/e/7/e76990b9-3372-4d88-8089-fce36e16744a/webperfection.net/sub/rady-nosu/wp-content/plugins/seo-ultimate/modules/class.su-module.php on line 1195
php7 coalesce operator ??

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

if you need to install PHP7 check out this article

Share This:

Comments are closed.