What is the function of the "??" operator (two questions) in PHP?

8

Looking at a script PHP at some point I came across this line

$valor = $_GET['id'] ?? 1;

What is the interpretation of this code? What does it do?

    
asked by anonymous 20.09.2017 / 15:51

2 answers

7

?? is known as null coalescing was added in the PHP 7 . Its functionality is to return the first operand if it exists and is not null otherwise the second returns.

The code in PHP 7

$valor = $_GET['id'] ?? 1;

Can be translated into PHP 5 as

 $valor = isset($_GET['id']) ? $_GET['id'] : 1;

Related:

What does the "?" operator mean in PHP?

Ternary operator "?:

What not to do : Is it possible to use the ternary operator in several conditions simultaneously?

    
20.09.2017 / 15:55
5

Same as in C # and other languages, this is #

$valor = isset($_GET['id']) ? $_GET['id'] : 1;
    
20.09.2017 / 15:55