6

In PHP7+ there is this new "Null coalescing operator." - http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op It says:

$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';

In that case, why does this code result in error/notice:

$username = (int)($_GET['user']) ?? 'nobody';

Notice: Undefined index: user

Is it the expected behavior (if so, doesn't it defy the purpose of this operator)? Similar notice when changing (int) to (string) etc.

Tomasz
  • 1,288
  • 18
  • 36

0 Answers0