Skip to content

Instantly share code, notes, and snippets.

@slav123
Last active August 13, 2019 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slav123/f5bfd9323e559de9402368130eed4a8d to your computer and use it in GitHub Desktop.
Save slav123/f5bfd9323e559de9402368130eed4a8d to your computer and use it in GitHub Desktop.
Null coalescing operator
<?php
// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
// Coalescing can be chained: this will return the first
// defined value out of $_GET['user'], $_POST['user'], and
// 'nobody'.
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment