Skip to content

Instantly share code, notes, and snippets.

@PHLAK
Created September 19, 2016 20:40
Show Gist options
  • Save PHLAK/4b12e7c6c6c2168dbee0c92a8c662787 to your computer and use it in GitHub Desktop.
Save PHLAK/4b12e7c6c6c2168dbee0c92a8c662787 to your computer and use it in GitHub Desktop.
Testing the difference between PHP's "??" and "?:" operators.
<?php
unset($foo);
var_dump($foo ?? 'bar'); // 'bar'
var_dump($foo ?: 'bar'); // 'bar' (PHP Notice: Undefined variable: foo ...)
$foo = 'foo';
var_dump($foo ?? 'bar'); // 'foo'
var_dump($foo ?: 'bar'); // 'foo'
$foo = [];
var_dump($foo ?? 'bar'); // []
var_dump($foo ?: 'bar'); // 'bar'
$foo = false;
var_dump($foo ?? 'bar'); // false
var_dump($foo ?: 'bar'); // 'bar'
$foo = null;
var_dump($foo ?? 'bar'); // 'bar'
var_dump($foo ?: 'bar'); // 'bar'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment