Skip to content

Instantly share code, notes, and snippets.

@bcremer
Last active August 29, 2015 14:07
Show Gist options
  • Save bcremer/7b05665c02799402e7ba to your computer and use it in GitHub Desktop.
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
<?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'
);
$ 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