Skip to content

Instantly share code, notes, and snippets.

@davidsword
Last active March 6, 2019 15:48
Show Gist options
  • Save davidsword/913bfb60b00642c74a1212d6bc360046 to your computer and use it in GitHub Desktop.
Save davidsword/913bfb60b00642c74a1212d6bc360046 to your computer and use it in GitHub Desktop.
PHP7 - New Feature! Null coalescing operator. Quickly do isset check and defines.
<?php
/**
* The new null coalescing operator! πŸŽ‰
* @see http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op
*/
$username = $_GET['user'] ?? 'nobody';
// The old way. πŸ™…
$username = isset( $_GET['user'] ) ? $_GET['user'] : 'nobody';
// The _really_ old way. πŸ™…
if ( isset( $_GET['user'] ) ) {
$username = $_GET['user'];
} else {
$username = 'nobody';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment