Last active
August 29, 2015 14:07
-
-
Save bcremer/7b05665c02799402e7ba to your computer and use it in GitHub Desktop.
PHP Null Coalesce Operator. Available since PHP7, see: https://wiki.php.net/rfc/isset_ternary
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 | |
error_reporting(-1); | |
ini_set('display_errors', 1); | |
$example = ['foo' => ['bar' => 'baz']]; | |
var_dump( | |
$example['foo'] ?? [] | |
); | |
var_dump( | |
$example['dummy'] ?? [] | |
); | |
var_dump( | |
$example['foo']['bar'] ?? 'else' | |
); | |
var_dump( | |
$example['foo']['dummy'] ?? 'else' | |
); |
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
$ build/php-src/sapi/cli/php -v | |
PHP 7.0.0-dev (cli) (built: Sep 29 2014 19:00:13) | |
Copyright (c) 1997-2014 The PHP Group | |
Zend Engine v2.8.0-dev, Copyright (c) 1998-2014 Zend Technologies | |
$ build/php-src/sapi/cli/php null_coalesce_operator_example.php | |
array(1) { | |
["bar"]=> | |
string(3) "baz" | |
} | |
array(0) { | |
} | |
string(3) "baz" | |
string(4) "else" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment