Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51908 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 18618 invoked from network); 15 Apr 2011 01:01:31 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Apr 2011 01:01:31 -0000 Authentication-Results: pb1.pair.com header.from=mail_ben_schmidt@yahoo.com.au; sender-id=unknown; domainkeys=good Authentication-Results: pb1.pair.com smtp.mail=mail_ben_schmidt@yahoo.com.au; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain yahoo.com.au from 98.139.53.198 cause and error) DomainKey-Status: good X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: mail_ben_schmidt@yahoo.com.au X-Host-Fingerprint: 98.139.53.198 nm12-vm0.bullet.mail.ac4.yahoo.com Received: from [98.139.53.198] ([98.139.53.198:39529] helo=nm12-vm0.bullet.mail.ac4.yahoo.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 84/57-59898-9E897AD4 for ; Thu, 14 Apr 2011 21:01:30 -0400 Received: from [98.139.52.190] by nm12.bullet.mail.ac4.yahoo.com with NNFMP; 15 Apr 2011 01:01:14 -0000 Received: from [98.139.52.141] by tm3.bullet.mail.ac4.yahoo.com with NNFMP; 15 Apr 2011 01:01:14 -0000 Received: from [127.0.0.1] by omp1024.mail.ac4.yahoo.com with NNFMP; 15 Apr 2011 01:01:14 -0000 X-Yahoo-Newman-Id: 781171.50501.bm@omp1024.mail.ac4.yahoo.com Received: (qmail 13891 invoked from network); 15 Apr 2011 01:01:14 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com.au; h=DKIM-Signature:Received:X-Yahoo-SMTP:X-YMail-OSG:X-Yahoo-Newman-Property:Message-ID:Date:From:User-Agent:MIME-Version:To:CC:Subject:References:In-Reply-To:Content-Type:Content-Transfer-Encoding; b=29FHxD2JkALaqDrYB+naoWdz6BWDV9U5U9QkbruFth5Zb9FYeX9hm6MDZ6ZIQIUyFAFYmZwuFPk3rpqfpRDWjJ8JvkIbgSOB831vxt0YBLSn66dYaHQa0Eu7npir6nhGrgWQSqNiog7TVIL0cjGrQOM1Np4P3dOR30X5nZWczb0= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com.au; s=s1024; t=1302829274; bh=ynsLBwvfeKUm6GVixwgbcCntePlczDjYVMDVTTxNHic=; h=Received:X-Yahoo-SMTP:X-YMail-OSG:X-Yahoo-Newman-Property:Message-ID:Date:From:User-Agent:MIME-Version:To:CC:Subject:References:In-Reply-To:Content-Type:Content-Transfer-Encoding; b=mNmxO7D4xT/QWYKQgCDy6HBDuyQMWcW+fXo+/Bk1zwXNacBPaZrERjLPNdYdz9n8b7NBky4h0kyztm+coljgxzy6b928iomcVcJTUF+RGPS48/eoCGAnZYltTvV4B6vPi2IIRm2VUJKV050L3evOXxcCPjz0dZcY4HocxQwEk/c= Received: from thought.local (mail_ben_schmidt@124.168.69.175 with plain) by smtp148.mail.mud.yahoo.com with SMTP; 14 Apr 2011 18:01:13 -0700 PDT X-Yahoo-SMTP: enFMnPSswBAexaHyzgobwuUTrYOhZdJ0KRA2SjA- X-YMail-OSG: X7wInjUVM1mi9YWrKgnTvOMYOl45zgOXq9jiqaheeIlaRyd 6lUbuo1FTP2ZM3Gx5QZraHTWUJdAOucHTFEG6ENqek8x02anO4OM.GgHXEB5 bnMhWtGLfFWQMwpi9N0Bb2H1qLpXxmfiYu5xq0znrD2XInonGmgqpIDatX4T 7V31uhfKUlzj7qdX9GEfOviUkipg5.4a1X.RiNxjDBRpqGiWBEntFLSk8YJX rU9ic7H9acBHpIzSWJUumSmkFwO_fj.aDDFpmDoCLkekz_r08xBkGrqgga6e ylnv.1IxCXvStEWMBsPoTjy29igzX2iz4Jw1okHu8tYK9uKHgB.G0w46NDbZ kDKN2jILKouYim.sfDLNT1wIAjA-- X-Yahoo-Newman-Property: ymail-3 Message-ID: <4DA798DE.1060805@yahoo.com.au> Date: Fri, 15 Apr 2011 11:01:18 +1000 User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-GB; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 ThunderBrowse/3.3.5 MIME-Version: 1.0 To: Hannes Landeholm CC: internals@lists.php.net References: <718216446.20110408143441@cypressintegrated.com> <4DA0E71C.9030008@gmail.com> <4DA63ED8.4080402@yahoo.com.au> <4DA6F2BC.10706@yahoo.com.au> <4DA6FB03.9040404@yahoo.com.au> <4DA703FB.3010603@yahoo.com.au> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Implicit isset/isempty check on short-ternary operator From: mail_ben_schmidt@yahoo.com.au (Ben Schmidt) I agree empty() is basically useless. We already have the existing ternary operator (and its shortcut) to do a boolean test, which is basically the same as empty(). The way I see it, if rather than making an isset() operator that suppresses errors and offers a default, we added both a !==null operator for the default, and a separate error-suppression mechanism, people could use the suppression mechanism with the existing boolean ternary operator if they want to (I would find that useful, as I often write things such as isset($a[$k])&&$a[$k]?"yes":"no" for that kind of thing), and use the !==null operator without error suppression (I would find that useful, too, to avoid typos). In summary, with two separate, simpler mechanisms, we could tackle these paradigms (I have used @[...] for undefined index error-suppression and $: for !==null default): 1. $v!==null ? $v : "default" $v $: "default" with notice 2. $a[$k]!==null ? $a[$k] : "default" $a[$k] $: "default" with notice 3. isset($a[$k]) ? $a[$k] : "default" $a@[$k] $: "default" without notice 4. isset($a[$k]) ? $a[$k] : null $a@[$k] without notice 5. isset($a[$k])&&!!$a[$k] !!$a@[$k] without notice 6. isset($a[$k])&&$a[$k] ? "yes" : "no" $a@[$k] ? "yes" : "no" without notice With !==null assignment (I've used $:=) we could also have: 7. if (!isset($a[$k])) $a[$k] = "default"; $a[$k] $:= "default"; without notice (the LHS of an assignment never generates one) To avoid encouraging poor coding, we would deliberately not have: 8. isset($v) ? $v : "default" $@v $: "default" without notice But it is a cinch to add it if enough people want it, and doing so wouldn't affect anyone who didn't want to use it--no backward compatibility problems on the horizon. I think that's the clincher. If we just add an isset() operator (that suppresses errors, and gives a default), we only get paradigms 3, 4, 5 and maybe 7, but worse, if we want to add any of the others later, we need to design more complicated new operators, or break backward compatibility, not just extend what we have. I personally use 1, 3, 5 and 6 quite often, and 2 and 7 occasionally, so I see great value in being able to do them all, not just the restricted set. What numbers are others interested in being able to solve? What do others think about the future-proofing issue? Ben. On 15/04/11 1:01 AM, Hannes Landeholm wrote: > I can agree that implementing ?? with isset() and not array_key_exists() would be > acceptable... but empty() is a blunt compromise and much less used... The general > problem is the notice being thrown when array indexes doesn't exist - which > results in code duplication when you deal with it nicely. empty() tries to be a > generic solution but there will always be people that has some other special > definition of "emptiness" like "array that contains a single null value" and they > need to write the code that defines that particular comparison anyway. > > You can't have a solution that makes everything easier for everyone so let's solve > one thing at a time and start with the most generic problem specifically and not > all minor problems that happens to partially intersect that one. > > ~Hannes > > > On 14 April 2011 16:26, Ben Schmidt > wrote: > > On 15/04/11 12:05 AM, Hannes Landeholm wrote: > > Trying to summarize this discussion... I think we can all agree that the > main problem is "code duplication for array access when parameters are > possibly not existing". > > > For me the problem is 'code duplication when a value might be null' > (whether an array, variable or something else, and regardless of whether > it was set to null, or not defined at all). > > > I think we all can also agree that @ can be both used properly and > misused - and it is a blunt tool and not a nice solution to the > previously stated problem. > > > Yes. > > > Some suggested that the ternary if comparison should suppress the notice > automatically. This would break existing code and also be confusing since > people expect a ternary if and normal if to work the same way. > > > Yes. > > > Some suggested ?? as an array access operator that suppresses the notice and > has 3 variants: A: nothing specified - uses null as default, B: has default > specified, C: returns X if index exists or Y if index doesn't exist. This > effectively solves the code duplication problem and is a shortcut for saying > "the array index may or may not exist". > > > This only works if the test is made an isset() kind of test. If it > remains a boolean cast, it doesn't help much. (Or at least it doesn't > help me much.) > > I also think it's a bit too blunt. In all but the simplest expressions > in the condition, desired notices could be silenced. I like the idea of > being able to specify exactly which array lookups should be silenced. > > It also doesn't help the people who want an isset() style test, but > without the notice suppression, and I think there are a few people in > that category. > > > One person said that the relation between ? and ?? and == and === would make > the operator non-intuitive. Other people disagreed and claimed the opposite. > > So basically the discussion now is what exact characters that should be used > to represent this operator? I really hope we can get this implemented > quickly... I worked with $_POST yesterday and I could really use that ?? > operator. > > > I still don't think we've reached agreement on exactly what we need. > Your summary seems to me to be of some of the earliest and least > developed ideas. I think the discussion has moved in a number of > interesting directions since then and we should draw on that later work. > > Ben. > > > >