9
Analyzing a script PHP at a certain point I came across this line
$valor = $_GET['id'] ?? 1;
What is the interpretation of this code? What does it do?
9
Analyzing a script PHP at a certain point I came across this line
$valor = $_GET['id'] ?? 1;
What is the interpretation of this code? What does it do?
8
The ??
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 returns the second operand.
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?
What not to do: It is possible to use the ternary operator under several conditions simultaneously?
5
Same as in C# and other languages, is the null-coalescing, but since null is a confusing concept in PHP, the check is whether the variable exists. It’s the same as writing:
$valor = isset($_GET['id']) ? $_GET['id'] : 1;
was worth the return, but I have no notion in C#
PHP documentation here
In PHP, at least Lvis Operator is the ?:
and behaves differently from null coalescing.
You don’t even have to, so all you needed to know is in the answer.
@Andersoncarloswoss changed there, again PHP being PHP.
Browser other questions tagged php operators php-7
You are not signed in. Login or sign up in order to post.
and this? $value = $_GET['id'] ?? $_POST['id'] ?? 1;
– user60252
@Leocaracciolo tries to get the value of a key called
id
first in$_GET
if there is no attempt at$_POST
if it is not found in the latter set as default value1
– rray
ball show :)
– user60252