# [RFC] Implicit isset() in Shorthand Ternary Operator

- Thread: https://staging.externals.io/message/77083
- Original archive: https://news-web.php.net/php.internals/77083
- Started by Andrea Faulds on 2014-09-06T00:02:45+00:00
- Messages: 34
- RFC: Null Coalesce Operator (Implemented (PHP 7.0)) — https://staging.externals.io/rfc/isset_ternary.md
- Page 1 of 2: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://staging.externals.io/message/77083.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://staging.externals.io/search.md?q={query} · RFCs at https://staging.externals.io/rfc.md · JSON API at https://staging.externals.io/api · documentation at https://staging.externals.io/llms.txt

---

## #77083 — Andrea Faulds — 2014-09-06T00:02:45+00:00

URL: https://staging.externals.io/message/77083#77083

Good evening,

I’ve written an RFC and working patch which attempt to add this feature which has been often requested: https://wiki.php.net/rfc/isset_ternary

Thanks!
--
Andrea Faulds
http://ajf.me/

---

## #77090 — Pierre Joye — 2014-09-07T10:30:32+00:00

URL: https://staging.externals.io/message/77083#77090 · In reply to: #77083

Hi Andrea,


>
> Good evening,
>
> I’ve written an RFC and working patch which attempt to add this feature
which has been often requested: https://wiki.php.net/rfc/isset_ternary

I did not look at the patch yet but I like it from a feature point of view.
We should have had this since long :)

Cheers,
Pierre

---

## #77091 — Sherif Ramadan — 2014-09-07T12:22:14+00:00

URL: https://staging.externals.io/message/77083#77091 · In reply to: #77090

I've played around with this branch for a bit and seems reasonable, passes
the tests, and doesn't seem to have any serious issues/memories leaks
AFAICT.

I can't stress enough, however, how important it is for us to avoid
inconsistencies in behavior between various forms of syntax. This is often
something users complain of as often they complain about not getting such
features. So let's make ternary consistent before we implement yet another
oddity in PHP (half-assed and ill designed).

I don't actually agree with the premise that ternary should wrap the first
operand in empty() to begin with, but if this is what people want, fine.
Let's just do it right.

And as Rasmus mentioned in the PR, make the documentation very _cleary_ if
it gets accepted.




> Hi Andrea,
>
> 
> > […]
> which has been often requested: https://wiki.php.net/rfc/isset_ternary
>
> I did not look at the patch yet but I like it from a feature point of view.
> We should have had this since long :)
>
> Cheers,
> Pierre

---

## #77092 — Andrea Faulds — 2014-09-07T12:25:53+00:00

URL: https://staging.externals.io/message/77083#77092 · In reply to: #77091

> I've played around with this branch for a bit and seems reasonable, passes
> the tests, and doesn't seem to have any serious issues/memories leaks
> AFAICT.

There’s actually a quite serious issue just now, which is that it evaluates the first operand twice if it’s not empty. This is because $a ?: $b is expanded to empty($a) ? $b : $a, such that if you did something which mutates state in there, it’d happen twice. This is obviously not good, and I’m going to figure out how to fix this.


> I can't stress enough, however, how important it is for us to avoid
> inconsistencies in behavior between various forms of syntax. This is often
> something users complain of as often they complain about not getting such
> features. So let's make ternary consistent before we implement yet another
> oddity in PHP (half-assed and ill designed).
> 
> I don't actually agree with the premise that ternary should wrap the first
> operand in empty() to begin with, but if this is what people want, fine.
> Let's just do it right.
> 
> And as Rasmus mentioned in the PR, make the documentation very _cleary_ if
> it gets accepted.

Yeah, I’ll be sure to make it crystal clear that they aren’t quite the same.

FWIW, it’s not our only shorthand which differs in behaviour. For example, ++ and += 1 are NOT the same, though I’d love to fix that particular inconsistency.

--
Andrea Faulds
http://ajf.me/

---

## #77097 — Simon Schick — 2014-09-08T07:58:02+00:00

URL: https://staging.externals.io/message/77083#77097 · In reply to: #77083

Hi, Andrea

I feel more like Sherif Ramadan. Even so I've quite often been in the same
situation, I don't think it's a good solution to change something like
that, just for the shorthand-operator.

I think, the notice is really valuable when accessing the value and doing
something with it - like myFunction($_GET['not_set']), but if you're just
checking for the existence, I, too, think it is more annoying. My way was
to suppress it by adding the silence-operator ;)

I always used like:
@$_GET['mykey'] ?: ””

I can't come up with a good mid-way right now, but I don't think it's a
good way to change this ONLY for the short-hand operator.

Bye,
Simon




> Good evening,
>
> I’ve written an RFC and working patch which attempt to add this feature
> which has been often requested: https://wiki.php.net/rfc/isset_ternary
>
> Thanks!
> --
> Andrea Faulds
> http://ajf.me/

---

## #77101 — Sherif Ramadan — 2014-09-08T08:47:45+00:00

URL: https://staging.externals.io/message/77083#77101

> 
>
> > […]
>
> This could very well be the reason why it is like it is today.
>
>
I agree. The more I think about, the more this seems weird. What if I
explicitly want to use ternary to check isset() and not empty?

---

## #77102 — Shashank Kumar — 2014-09-08T12:04:12+00:00

URL: https://staging.externals.io/message/77083#77102 · In reply to: #77101

Hi Andrea

Rather than giving a new meaning to an old operator why not have a
different operator for this?
.NET has a 'null coalescing' operator for the same purpose which works out
quite well in the given situation
and is non-ambiguous as well.
http://msdn.microsoft.com/en-us/library/ms173224.aspx
​

---

## #77103 — Andrea Faulds — 2014-09-08T13:58:17+00:00

URL: https://staging.externals.io/message/77083#77103

> 
> > […]
> 
> This could very well be the reason why it is like it is today.

I doubt it, it’s possible to avoid double-evalution, and I don’t think it’s terribly difficult. I just did things the easy way and it turns out that has problems.




> Rather than giving a new meaning to an old operator why not have a different operator for this?
> .NET has a 'null coalescing' operator for the same purpose which works out quite well in the given situation 
> and is non-ambiguous as well.
> http://msdn.microsoft.com/en-us/library/ms173224.aspx

We could add such an operator, perhaps with the ?? syntax. However, I don’t really like the idea. It’s too similar to ?: so I don’t think it’d be accepted, and even if it was, I’m not sure we really need another operator. I’d much rather just make ?: do what, IMO, is the right thing and what it always should have done.



> I feel more like Sherif Ramadan. Even so I've quite often been in the same situation, I don't think it's a good solution to change something like that, just for the shorthand-operator.
> 
> I think, the notice is really valuable when accessing the value and doing something with it - like myFunction($_GET['not_set']), but if you're just checking for the existence, I, too, think it is more annoying. My way was to suppress it by adding the silence-operator ;)
> 
> I always used like:
> @$_GET['mykey'] ?: ””

The silence operator is bad for three reasons. First, it’s very slow, having a significant performance impact. Second, @ silences not only innocuous E_NOTICEs, but *all* errors, even very serious ones. That’s not good. The third is that it interacts with some error handlers badly, or so I’ve heard.

> I can't come up with a good mid-way right now, but I don't think it's a good way to change this ONLY for the short-hand operator.

Actually, there is a case for doing it for $a ?: $b and not $a ? $a : $b, in that it could avoid double evaluation (you’d be able to do foo()[0] ?: $bar and foo() would only be called once) and because with the full-length ternary operator, it wouldn’t really help you much: $_GET[‘foo’] ? $_GET[‘foo’] : ‘blah’ isn’t much shorter than the version with explicit isset.

--
Andrea Faulds
http://ajf.me/

---

## #77105 — Adam Harvey — 2014-09-08T22:58:00+00:00

URL: https://staging.externals.io/message/77083#77105

> Am 08.09.2014 15:58, schrieb Andrea Faulds:
> > […]
>
> I'd rather had a shortcut for the following:
>
>   isset($_GET['foo']) ? $_GET['foo'] : BAR

Agreed. That's what ifsetor requests have generally boiled down to
over the years, so it seems to be what the masses want.

It's what _I_ want, anyway. :)

> Of course, it is not possible to change the ?: operator to work this way
> for BC reasons, but a new operator such as ?? might make sense.

+1 on ?? — there's precedent for it, and it means we don't have to
explain why the shorthand form of an operator behaves differently to
the long form, which is just going to confuse users.

Adam

---

## #77106 — Andrea Faulds — 2014-09-09T00:07:59+00:00

URL: https://staging.externals.io/message/77083#77106 · In reply to: #77105

> +1 on ?? — there's precedent for it, and it means we don't have to
> explain why the shorthand form of an operator behaves differently to
> the long form, which is just going to confuse users.

FWIW, it already behaves differently:

    oa-res-27-90:~ ajf$ php -r 'function foo() { echo "foo\n"; return true; } $x = foo() ?: false;'
    foo
    oa-res-27-90:~ ajf$ php -r 'function foo() { echo "foo\n"; return true; } $x = foo() ? foo() : false;'
    foo
    foo
--
Andrea Faulds
http://ajf.me/

---

## #77107 — Adam Harvey — 2014-09-09T00:16:02+00:00

URL: https://staging.externals.io/message/77083#77107 · In reply to: #77106

>
> 
>
> > […]
>
> FWIW, it already behaves differently:
>
>     oa-res-27-90:~ ajf$ php -r 'function foo() { echo "foo\n"; return true; } $x = foo() ?: false;'
>     foo
>     oa-res-27-90:~ ajf$ php -r 'function foo() { echo "foo\n"; return true; } $x = foo() ? foo() : false;'
>     foo
>     foo

You could argue whether that's unexpected behaviour or not — there is
only one foo() call in the shorthand version, after all, so it makes
intuitive sense that foo() would only be called once.

Regardless of how you feel about that, though, I don't think
increasing the amount of inconsistency between two versions of the
same operator (forget the implementation; that's how it's documented
and how it's perceived) is a good idea.

Adam

---

## #77108 — Andrey Andreev — 2014-09-09T00:16:47+00:00

URL: https://staging.externals.io/message/77083#77108 · In reply to: #77106

>
> 
>
> > […]
>
> FWIW, it already behaves differently:
>
>     oa-res-27-90:~ ajf$ php -r 'function foo() { echo "foo\n"; return true; } $x = foo() ?: false;'
>     foo
>     oa-res-27-90:~ ajf$ php -r 'function foo() { echo "foo\n"; return true; } $x = foo() ? foo() : false;'
>     foo
>     foo

That's arguable ... I'd say that ($x = foo()) ? $x : false; is the
logical equivalent of your first example.

Cheers,
Andrey.

---

## #77113 — rparker@yamiko.org — 2014-09-09T17:08:36+00:00

URL: https://staging.externals.io/message/77083#77113

From: Andrey Andreev
Sent: ‎Monday‎, ‎September‎ ‎8‎, ‎2014 ‎5‎:‎16‎ ‎PM
To: Andrea Faulds
Cc: Adam Harvey, Christoph Becker, PHP internals





>
> > […]
>
>That's arguable ... I'd say that ($x = foo()) ? $x : false; is the
>logical equivalent of your first example.
>
>Cheers,
>Andrey.



I agree and think we should go with a different operator as others suggested. If not ?? than we could use ?=  like $foo ?= “default string”.

---

## #77115 — Rowan Collins — 2014-09-09T18:27:01+00:00

URL: https://staging.externals.io/message/77083#77115 · In reply to: #77113

> I agree and think we should go with a different operator as others suggested. If not ?? than we could use ?=  like $foo ?= “default string”.

That reads like an assignment, rather than an expression - particularly 
given PHP's large number of evaluate-and-assign operators (+=, *=, etc).

Actually, that might be a useful addition in its own right.

For a full set, we could have the following:

// Evaluation as boolean:
$bar = (bool)$bar ? $bar : $baz;
$bar = $bar ? $bar : $baz;
$bar = $bar ?: $baz;
$bar ?:= $baz;

// Test for null  / isset:
$bar = isset($bar) ? $bar : $baz;
$bar = $bar ?? $baz;
$bar ??= $baz;

Not the most readable, perhaps, but then "shorthand" and "readable" are 
often in conflict with one another.

Regards,

-- 
Rowan Collins
[IMSoP]

---

## #77135 — Pierre Joye — 2014-09-10T09:31:44+00:00

URL: https://staging.externals.io/message/77083#77135 · In reply to: #77105

> 
> > […]
>
> Agreed. That's what ifsetor requests have generally boiled down to
> over the years, so it seems to be what the masses want.
>
> It's what _I_ want, anyway. :)
>
> > […]
>
> +1 on ?? — there's precedent for it, and it means we don't have to
> explain why the shorthand form of an operator behaves differently to
> the long form, which is just going to confuse users.

After a 2nd look I have to agree here too. Changing behavior in
something so widely used as the current operator will likely create
more pains. A new operator, clearly documented, sounds much cleaner,
even more as it does something different anyway.


-- 
Pierre

@pierrejoye | http://www.libgd.org

---

## #77265 — Andrea Faulds — 2014-09-16T18:34:02+00:00

URL: https://staging.externals.io/message/77083#77265 · In reply to: #77135

> 
> > […]
> 
> After a 2nd look I have to agree here too. Changing behavior in
> something so widely used as the current operator will likely create
> more pains. A new operator, clearly documented, sounds much cleaner,
> even more as it does something different anyway.

By popular demand, I’ve changed the RFC to instead propose a ?? operator, after Nikita Popov generously donated a working ?? patch. In doing so, the RFC is renamed “Null Coalesce Operator”.

Please read it: https://wiki.php.net/rfc/isset_ternary

Thanks!
--
Andrea Faulds
http://ajf.me/

---

## #77266 — Adam Harvey — 2014-09-16T18:38:37+00:00

URL: https://staging.externals.io/message/77083#77266 · In reply to: #77265

> By popular demand, I’ve changed the RFC to instead propose a ?? operator, after Nikita Popov generously donated a working ?? patch. In doing so, the RFC is renamed “Null Coalesce Operator”.
>
> Please read it: https://wiki.php.net/rfc/isset_ternary

Love it! Kudos to you and Nikita.

Is it possible to chain ?? operators; ie $value = $foo ?? $bar ?? 'default'?

Adam

---

## #77267 — Andrea Faulds — 2014-09-16T18:42:36+00:00

URL: https://staging.externals.io/message/77083#77267 · In reply to: #77266

> 
> > […]
> 
> Love it! Kudos to you and Nikita.
> 
> Is it possible to chain ?? operators; ie $value = $foo ?? $bar ?? 'default’?

Yep:

$ sapi/cli/php -r '$x = NULL; $y = NULL; $z = 3; var_dump($x ?? $y ?? $z);'
int(3)

$ sapi/cli/php -r '$x = ["yarr" => "meaningful_value"]; var_dump($x["aharr"] ?? $x["waharr"] ?? $x["yarr"]);'
string(16) "meaningful_value"
--
Andrea Faulds
http://ajf.me/

---

## #77268 — Shashank Kumar — 2014-09-16T18:55:43+00:00

URL: https://staging.externals.io/message/77083#77268 · In reply to: #77267

This will be a nice addition. Kudos to you!

Is there an issue targeting next minor for this? (assuming there is one)
​

---

## #77269 — Andrea Faulds — 2014-09-16T18:57:44+00:00

URL: https://staging.externals.io/message/77083#77269 · In reply to: #77268

> This will be a nice addition. Kudos to you!

Really, you should thank Nikita, not me. He wrote the patch. ^^

> Is there an issue targeting next minor for this? (assuming there is one)

Well, the patch is written for master. It could be back ported, but I don’t know how.

Also, even if there is a 5.7, it sounds like it will consist purely of bug fixes and `E_DEPRECATED` warnings.

--
Andrea Faulds
http://ajf.me/

---

## #77271 — Shashank Kumar — 2014-09-16T19:20:08+00:00

URL: https://staging.externals.io/message/77083#77271 · In reply to: #77269

> Really, you should thank Nikita, not me. He wrote the patch. ^^
​He gets my bag of thanks for many things.

> Well, the patch is written for master. It could be back ported, but I
don’t know how.
> Also, even if there is a 5.7, it sounds like it will consist purely of
bug fixes and `E_DEPRECATED` warnings.
Backward-compatible feature additions won't harm 5.7 and probably be a good
upgrade path to 7 but that's a discussion
for some future time.
Given that accepted intdiv rfc is targeted to 5.7 I thought it will be ok
here as well.

---

## #77276 — Kingsquare.nl - Robin Speekenbrink — 2014-09-16T20:46:21+00:00

URL: https://staging.externals.io/message/77083#77276

2014-09-16 22:00 GMT+02:00 Christoph Becker <cmbecker69@gmx.de>:
> Andrea Faulds wrote:
>
> > […]
>
> +1
>
> > […]
>

Hi,

As a userland point of view on this: will this have a shorthand? i.e.
what will happen if i leave out the second part?

ie.
$var = $_GET['test'] ?? ;

would that be the same as
$var = @$_GET['test'];

The RFC isnt clear in this regard (or that the righthand side of the
operator _is_ required... maybe it's me ?

Regards

Robin Speekenbrink

---

## #77277 — Andrea Faulds — 2014-09-16T21:00:52+00:00

URL: https://staging.externals.io/message/77083#77277 · In reply to: #77276

> As a userland point of view on this: will this have a shorthand?

No.

> i.e.
> what will happen if i leave out the second part?
> 
> ie.
> $var = $_GET['test'] ?? ;
> 
> would that be the same as
> $var = @$_GET['test’];

No, that would be a syntax error.
--
Andrea Faulds
http://ajf.me/

---

## #77281 — Sara Golemon — 2014-09-17T08:58:32+00:00

URL: https://staging.externals.io/message/77083#77281 · In reply to: #77276

> 
> 
> As a userland point of view on this: will this have a shorthand? i.e.
> what will happen if i leave out the second part?
> 
> ie.
> $var = $_GET['test'] ?? ;
> 
> would that be the same as
> $var = @$_GET['test'];
> 
> The RFC isnt clear in this regard (or that the righthand side of the
> operator _is_ required... maybe it's me ?
> 
The equivalent of your error suppressed example would be:
$var = $_GET['test'] ?? null;

I don't think it gains anyone much to have another form just to save four characters (for the default `null`).

Someone mentioned an assignment version which isn't covered in niki's patch but would be a fairly trivial addition.

$var ??= expr;

Which essentially means:
if (!isset($var)) {
  $var = expr;
}

I'd kinda like that for completeness, but will vote for this with or without the assignment version.

-Sara

---

## #77285 — Kingsquare.nl - Robin Speekenbrink — 2014-09-17T13:41:57+00:00

URL: https://staging.externals.io/message/77083#77285 · In reply to: #77281

2014-09-17 10:58 GMT+02:00 Sara Golemon <php@golemon.com>:
>
> > […]
> The equivalent of your error suppressed example would be:
> $var = $_GET['test'] ?? null;
>
> I don't think it gains anyone much to have another form just to save four characters (for the default `null`).
>
> Someone mentioned an assignment version which isn't covered in niki's patch but would be a fairly trivial addition.
>
> $var ??= expr;
>
> Which essentially means:
> if (!isset($var)) {
>   $var = expr;
> }
>
> I'd kinda like that for completeness, but will vote for this with or without the assignment version.
>
> -Sara

Sara,

Indeed that's what i meant. I'm afraid that i just wasn't clear the first time.
But would indeed also like to see the ??= in one go. This would make
the operator a direct first-class citizen of the language instead of
halfway there ;)

Thanks again, i'll butt out now ;)

Robin

---

Next page: https://staging.externals.io/message/77083.md?page=2
