Skip to content

Conversation

@hikari-no-yume
Copy link
Contributor

The patch for this RFC: https://wiki.php.net/rfc/isset_ternary

@lstrojny
Copy link
Contributor

Cool!

@laruence
Copy link
Member

I like the idea... but still think ?? is ugly :<

@shoomyth
Copy link

Maybe ??:?

$a = isset($b) && $b ? 'b' : 'c';
$a = $b ?? 'b' : 'c';

$a = isset($b) && $b ? $b : 'c';
$a = $b ??: 'c';

@rquadling
Copy link
Contributor

Considering that the basic purpose of a coalesce mechanism is to group questions and ORs...

$a = $b ?| $c ?| $d ?| 'default';

should be the equivalent of ...

$a = (isset($b) && !!$b) ? $b : (isset($c) && !!$c ? $c : (isset($d) && !!$d ? $d : 'default'));

As ? is a question mark and | is the OR symbol, combining them feels appropriate.

I think using just symbols, we are going to be on a limited choice. Combining symbols would seem to be the only choice. And so, if the symbols used can relate to the use, then that should make it easier to understand.

@hikari-no-yume
Copy link
Contributor Author

I agree in principle, but ?| is ugly.

@rquadling
Copy link
Contributor

How about |? ? :-D

@hikari-no-yume
Copy link
Contributor Author

No.

@shoomyth
Copy link

@TazeTSchnitzel what about ??: syntax?

@nikic
Copy link
Member

nikic commented Sep 17, 2014

How about $foo ?!?!?! $bar?

@hikari-no-yume
Copy link
Contributor Author

@nikic Excellent, let's ship it.

@morrisonlevi
Copy link
Contributor

Let's just be honest here and say that ?? has a precedent in C# and Swift. Some other languages use ?: such as Cold Fusion and Objective-C but we already threw that option out the window. I see no reason to deviate from precedent in this case.

@hikari-no-yume
Copy link
Contributor Author

@morrisonlevi C-C-C-C-C-C-C-C-C-COMBO BREAKERRRR

@shoomyth
Copy link

?? looks like WTF?! or I have a question in this line :)

@hikari-no-yume
Copy link
Contributor Author

@shoomyth Well, WTF?! No: Stop is already valid PHP code.

@pk-fr
Copy link

pk-fr commented Sep 18, 2014

ok for
$a = $b ?? "something"; // equivalent to $a = isset($b) ? $b : "something"

so why not add also?

$a ??= "something"; // which will be equivalent to $a = isset($a) ? $a : "something" ...

@Majkl578
Copy link
Contributor

shoomyth said:
@TazeTSchnitzel what about ??: syntax?

👍

I already suggested it in original thread.
$foo ?? exists : nope and $foo ??: nope would be perfectly consistent with current ternary operator (and make sense as well).

@hikari-no-yume
Copy link
Contributor Author

Please stop making the same suggestions, it's hurting my sanity.

@datibbaw
Copy link
Contributor

Could we add another test case?

function f($x)
{
    printf("%s(%d)\n", __FUNCTION__, $x);
    return $x;
}

$a = f(null) ?? f(1) ?? f(2);

Should output f(0)\nf(1)\n, as in it doesn't invoke f(2).

@hikari-no-yume
Copy link
Contributor Author

@datibbaw Sure thing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stdClass

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Class names are case-insensitive, it really doesn't matter.

@hikari-no-yume
Copy link
Contributor Author

@datibbaw Done.

@php-pulls php-pulls merged commit ec39802 into php:master Sep 27, 2014
@adnen-chouibi
Copy link

adnen-chouibi commented Jan 28, 2023

"string" . $array['undefined_key'] ?? "" . " another string"
-> Warning: Undefined array key "undefined_key" //expected to return "string another string"
$array['exist_key'] ?? "" . "another string"
-> return the value of $array['exist_key'] //expected to return $array['key']."another string"

@hikari-no-yume
Copy link
Contributor Author

@adnen-chouibi try putting parentheses around the part following the period

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.