Re: Implicit isset/isempty check on short-ternary operator
| From: | Ben Schmidt | Date: | Wed, 20 Apr 2011 14:09:54 +0000 |
| Subject: | Re: Implicit isset/isempty check on short-ternary operator | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-51949@lists.php.net to get a copy of this message | ||
On 17/04/11 9:14 AM, Ángel González wrote:
Ben Schmidt wrote: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.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.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.$var = $arr['key'] ?? : 'empty';