Ternary operator " ?: "

Asked

Viewed 472 times

2

I recently got into a discussion with colleagues about the more compact functionality of the if ternary ( x ?: 10) and some of them said it would be removed in php 7. I did a web search and found nothing related to it.

Is there any information about it?

  • 3

    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.

  • 2

    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.

  • 1

    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.

  • 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.

  • 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.

  • 1

    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 :/

Show 1 more comment

1 answer

8


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.

  • That’s exactly it. I believe my colleague was wrong about removing the functionality. I got the flea behind my ear because he said he heard this information in a lecture about php recently.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.