Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51827 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 55657 invoked from network); 7 Apr 2011 21:59:43 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 7 Apr 2011 21:59:43 -0000 X-Host-Fingerprint: 208.107.178.23 host-23-178-107-208.midco.net Date: Thu, 07 Apr 2011 17:59:43 -0400 Received: from [208.107.178.23] ([208.107.178.23:8986] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 69/82-36433-EC33E9D4 for ; Thu, 07 Apr 2011 17:59:43 -0400 Message-ID: <69.82.36433.EC33E9D4@pb1.pair.com> To: internals@lists.php.net References: <4D950434.3060704@yahoo.com.au> <4D9E0543.1080600@lerdorf.com> User-Agent: slrn/pre1.0.0-18 (Linux) X-Posted-By: 208.107.178.23 Subject: Re: [PHP-DEV] Implicit isset/isempty check on short-ternary operator From: weierophinney@php.net (Matthew Weier O'Phinney) On 2011-04-07, Rasmus Lerdorf wrote: > On 4/7/11 2:30 PM, Rafael Dohms wrote: > > On Thu, Mar 31, 2011 at 7:46 PM, Ben Schmidt > > wrote: > > > On 1/04/11 3:29 AM, David Coallier wrote: > > > > 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: > > > > > > > > > > > $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. > > > > > > > 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. > > Well, it would change the semantics. And Ben, no, it isn't shorthand for > an isset() it is like writing (boolean)$var?$var:$something It may change the semantics as they stand, but I'd argue that the _expectation_ from the shorthand ternary is to shorten code that currently uses isset(). As it is, I have almost no use for it at this point, as I end up needing to do: $value = isset($a[$key]) ? $a[$key] : 'Not set'; which is exactly the situation I had before it was introduced. -- Matthew Weier O'Phinney Project Lead | matthew@zend.com Zend Framework | http://framework.zend.com/ PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc