Created
September 19, 2016 20:40
-
-
Save PHLAK/4b12e7c6c6c2168dbee0c92a8c662787 to your computer and use it in GitHub Desktop.
Testing the difference between PHP's "??" and "?:" operators.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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