On 17/04/11 9:14 AM, Ángel González wrote:
Ben Schmidt wrote:
$var = $arr['key'] ?? : 'empty';

Also note this is possible with the recent proposal Hannes and I were
discussing. It simply looks like

$var = $arr?['key'] ?: 'empty';

The ?[ avoids notices and the ?: works as it always has.

Ben.

If it was going to be ?[, I'd much prefer $arr['key'?]. It was proposed as
$arr[?'key'] instead to avoid a backtracking (would that really be
noticeable?
I'd prefer readibility), but ?[ would have the same problem.

The backtracking issue was part of a different proposal where a ternary
operator had an optional 'else' part. It is not an issue with
notice-free array lookups as more recently proposed, and I see no reason
we couldn't use ?[ or [? or ?] as the syntax. In all cases, either that
combination of characters can be scanned as a single token, or the
parsing can be done with just one token of lookahead and no
backtracking. I don't know what kind of parser PHP uses, though, so I'm
just guessing; but AFAICT any standard kind of parser shouldn't have
much trouble here.

The one argument I can find for favouring ?[ or [? is that this
generalises more naturally. Perhaps [? is the best. It generalises
nicely if required:

$array[?'key']
$?variable
$object->?property

I personally only think we need the first one, but there are a few votes
for the second, too. I personally also don't care very much about the
syntax; I'd be equally happy with $array['key'?] or $array?['key']. I'm
just keen to have the functionality (and separately, the !==null
operator(s)).

Ben.




--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to