Skip to content

Instantly share code, notes, and snippets.

@Chemaclass
Created September 21, 2019 22:51
Show Gist options
  • Select an option

  • Save Chemaclass/56f3f4e405bb780a9bde41060e031f89 to your computer and use it in GitHub Desktop.

Select an option

Save Chemaclass/56f3f4e405bb780a9bde41060e031f89 to your computer and use it in GitHub Desktop.
Docu - 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