Re: Implicit isset/isempty check on short-ternary operator
| From: | Ben Schmidt | Date: | Fri, 08 Apr 2011 02:49:09 +0000 |
| Subject: | Re: Implicit isset/isempty check on short-ternary operator | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-51832@lists.php.net to get a copy of this message | ||
On 8/04/11 4:41 AM, Rasmus Lerdorf wrote:
On 4/7/11 2:30 PM, Rafael Dohms wrote:Thanks for the clarification, Rasmus. That makes sense. I still think the patch would be very useful, though I can also see how an argument could be made for keeping the warning. In my opinion, the case for removing the warning is stronger than for keeping it. Ben.On Thu, Mar 31, 2011 at 7:46 PM, Ben Schmidt <mail_ben_schmidt@yahoo.com.au> wrote:Well, it would change the semantics. And Ben, no, it isn't shorthand for an isset() it is like writing (boolean)$var?$var:$somethingOn 1/04/11 3:29 AM, David Coallier wrote:Its also a +1 for me, it would make the ternary operator much more useful to me e remove lots of verbose code for handling arrays.Hey there, I've been working on a little patch that will allow variables ($1) in a short-ternary operation to go through an implicit isset (zend_do_isset_or_isempty) check so that the average use-case for the short-ternary operator doesn't yield in a notice whenever the first variable of the expression isn't set. So the use-case I'm considering is the following: <?php $list = array('name' => 'list'); echo $list['name'] ?: 'List not set.' . PHP_EOL; echo $list['name-notset'] ?: 'List not set.' . PHP_EOL; ?> This example, which we all know, will result in a notice for the second echo statement because the requested array-key doesn't actually exist in the array.I'm not familiar with the construct, but to be honest, I fail to see how it is useful without the suppression of the notice. I mean, it's a shorthand for isset($var)?$var:$something_else isn't it? That presupposes that $var may not be set, but that you've accounted for that fact, so don't want a notice about it. Obviously the isset() can only be applied if the 'left hand side' is an lval, but I think it makes sense to apply it whenever it is an lval. So a big +1 from me. Ben.