The name of that operator is Lvis Operator.
It still exists in PHP 7. It may be just a matter of compatibility or even for comfort for those who are used to it, but only those who develop the language can answer for sure.
In PHP 7 there is null coalescing Operator (??
) and this operator does the same thingsee edition.
The main difference between them is that the null coalesces will not generate a E_NOTICE
when the variable is not defined.
$a = null;
print $a ?? 'não há valor'; // Saída: não há valor
print $a ?: 'não há valor'; // Saída: não há valor
print $b ?? 'não há valor'; // Saída: não há valor
print $b ?: 'não há valor'; // Notice: Undefined variable: b
Edit:
They do not exactly the same thing, there is an important difference not yet mentioned. The operator null coalescing (??
) evaluates its second expression only if the first expression is null
or not yet awarded. Ex.:
null ?? 'Teste' == 'Teste';
Already the operator Elvis evaluates the second expression only if the first expression is falsy, that is, a value that, if converted to boolean
, has the value false
. See this answer with a more detailed explanation. Ex.:
0 ?: 'Teste' == 'Teste';
Some examples showing the difference between operators:
null ?: 'Nope' == Nope
null ?? 'Nope' == Nope
'' ?: 'Nope' == Nope
'' ?? 'Nope' == ''
' ' ?: 'Nope' == ' '
' ' ?? 'Nope' == ' '
false ?: 'Nope' == Nope
false ?? 'Nope' == false
0 ?: 'Nope' == Nope
0 ?? 'Nope' == 0
1 ?: 'Nope' == 1
1 ?? 'Nope' == 1
[] ?: 'Nope' == Nope
[] ?? 'Nope' == []
Repl.it with the code working.
I don’t even know what to do with this question. I closed now as unclear for missing information about it. If it’s just brainless rumor I think it’s out of scope, if it’s something to take seriously maybe you can answer or maybe it’s based on opinion, or even duplicate since the subject, in general, has been addressed a few times.
– Maniero
I think he just wanted to know if the operator was taken out on PHP7 (it wasn’t), because it seems that colleagues got confused with the introduction of the
Null Coalescing
.– Ricardo Moraleida
At least based on opinions. There is no record of the removal of the operator in any part of the documentation, even though it is updated to version 7. So much so that the information about that operator is next to the
null coallesce
, who must have been confused.– Marcelo de Andrade
To this day they have not removed the keyword
var
or the behavior of the constructor with the same class name. It takes a few eras for development personnel to take away anything in the language.– rray
So during the discussion one of them said that they said this information in a talk about PHP. I did a search and found nothing saying that this feature would be taken out in PHP 7. And I also found it strange because of the null Coalescing insertion, which is an improvement of Elvis Perator. I decided to ask to know if anyone else had this information of the withdrawal of functionality.
– Arthur Moro
People love to make up rumors based on bits of information, I remember the time they said that require was slow and include fast. PHP is a rumor zone by developers unfortunately :/
– Guilherme Nascimento