FAQ

On Nov 26, 2013, at 12:41, Paul Dragoonis wrote:
On Tue, Nov 26, 2013 at 4:58 PM, Chris London wrote:
On Tue, Nov 26, 2013 at 8:15 AM, Mats Lindh wrote:
On Tue, Nov 26, 2013 at 3:43 PM, Chris London wrote:

I believe these two statements are functionally equivalent:

$foo = $foo ? $foo : 'default';
$foo = $foo ?: 'default';
They are.

I would like to change it so it also checks for isset() so I propose the
following would be functionally equivalent:

$foo = isset($foo) && $foo ? $foo : 'default';
$foo = $foo ?: 'default';
We can't change the behaviour of ?: that ship has already sailed, we'll
just be hurting existing codebases on upgrading and more user education is
required to understand non-visual changes in syntax like this, lets try to
learn from our mistakes by not changing 'behaviour' anymore.

An alternative syntax that is similar and not ugly would be good since the
?: behaviour was broken from the start since you still need to run isset()
before running ?: which was the problem we tried to solve in the first
place but it just didn't happen.

I don't like the @ symbol it's too different from what's already there on
the ternary logic.

Proposal: $foo = $arr['value'] ?:: "";

Thoughts?

What if it was:

$foo ?? $trueVal : $falseVal;

with the shortened form being:

$foo ??: $falseVal;

This would make the isset($foo) && $foo functionality available for the three operand version of ??:, and by making the ? the operator that's doubled in that case would make it more clear when reading as to what's going on (and maybe make it easier to parse?).

-John

The would break the assumption that a reference to an uninitialized value
would generate a notice, unless explicitly handled in the logic.

While I also would like to have something similar to ?: to handle default
values for array keys, etc., this would change a fundamental assumption
that as been in place for many years now. I'm not sure if that's a BC break
that would be acceptable this late. An alternative operator may be more
suitable.
What do you think about using:

$foo = $foo @: 'default';

and possibly

$foo @= 'default';


The change proposed has also been discussed several times since the
implementation of ?:. See the ifsetor-RFC:

https://wiki.php.net/rfc/ifsetor

It also contains links to the discussion around the feature back then.

--mats

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 14 of 23 | next ›
Discussion Overview
groupphp-internals @
categoriesphp
postedNov 26, '13 at 2:43p
activeNov 26, '13 at 9:52p
posts23
users12
websitephp.net

People

Translate

site design / logo © 2022 Grokbase