Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51842 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 69353 invoked from network); 8 Apr 2011 15:47:52 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Apr 2011 15:47:52 -0000 Authentication-Results: pb1.pair.com header.from=landeholm@gmail.com; sender-id=pass; domainkeys=bad Authentication-Results: pb1.pair.com smtp.mail=landeholm@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.210.170 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: landeholm@gmail.com X-Host-Fingerprint: 209.85.210.170 mail-iy0-f170.google.com Received: from [209.85.210.170] ([209.85.210.170:41517] helo=mail-iy0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id FC/70-65418-52E2F9D4 for ; Fri, 08 Apr 2011 11:47:50 -0400 Received: by iyb12 with SMTP id 12so4403798iyb.29 for ; Fri, 08 Apr 2011 08:47:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=rA8bU8cpJ0t2MSqvnVcLygg35mwKcneEOw7gomDqqFQ=; b=GXPlwoenXrS0gMnjQ45Rhw4l/WnOGciv01tAeAH7ioRSyqOvKyZSN7IkyT8fRiRlNf 0MRBGkZPbkqWEZR0PCrvMzIFSlG3BwsgHPMZQkZxRKJhMglzthainLo+jBD1g85tkvcv StT4XR2G2fPmDQGGoBhyfmeaLQHO0bnXYxbos= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=nddpVm8eLz8AqclnKg4fNwBRO3zcYgu1j9XtvVcdiMPOkLmOMVlOl9Tn9DfmQV41Rf 3NsXVcUkEJHHKL/Cmdu1Jx7bkAuq2PnzpaiiQXR6cw/AQQPXL/49OGLNTXyTfvcdBHjR qaXKTygT3q0t3Dn5R9JrHUxVWC+c2fMs0yVxc= MIME-Version: 1.0 Received: by 10.43.55.83 with SMTP id vx19mr4005476icb.24.1302277667036; Fri, 08 Apr 2011 08:47:47 -0700 (PDT) Received: by 10.231.39.132 with HTTP; Fri, 8 Apr 2011 08:47:46 -0700 (PDT) In-Reply-To: References: <4D950434.3060704@yahoo.com.au> <4D9E0543.1080600@lerdorf.com> <69.82.36433.EC33E9D4@pb1.pair.com> <4D9E34C4.5000406@lerdorf.com> <4D9E429B.20503@sugarcrm.com> <4D9E96B6.6060401@lerdorf.com> Date: Fri, 8 Apr 2011 17:47:46 +0200 Message-ID: To: Martin Scotta , internals@lists.php.net Content-Type: multipart/alternative; boundary=bcaec517a9aebe1e2f04a06a2535 Subject: Re: [PHP-DEV] Implicit isset/isempty check on short-ternary operator From: landeholm@gmail.com (Hannes Landeholm) --bcaec517a9aebe1e2f04a06a2535 Content-Type: text/plain; charset=ISO-8859-1 Operators should have very specific purpose and function... a ternary if statement should just be another way to express a normal if statement. Making the ? operator suppress the not defined error would be a poor compromise, making PHP inconsistent. Then I'd rather have access of non-defined array indexes return null instead (simply removing the not-defined notice for arrays - where array_key_exists and isset would still work like normal if you want to check for undefined). ~Hannes On 8 April 2011 16:45, Martin Scotta wrote: > I just feels that !empty($arr['key']) or isset($arr['key']) do not express > the real meaning, instead I would choose to write array_key_exists('key', > $arr). It may be slower but it clearly express what I meant. > > Regarding the operators, I believe they will do more harm than good. > To check if a variable was already defined is a bad programing habit, these > operators will encourage that kind of things > > In the other hand, the isset check inside the condition ?: would silently > improve not-so-well written code, and could be a good feature for the lang. > > Martin Scotta > > > > On Fri, Apr 8, 2011 at 11:27 AM, Hannes Landeholm wrote: > >> I think "?!" wouldn't work as an operator as it would conflict with >> ternary >> comparision + not operator. Also I don't see the point of adding an >> operator >> for "empty" as the function/construct itself is pretty confusing and >> non-useful as you have to memorize all the things that happen to be >> considered "empty" by it and those things has too align up with the things >> you want to check for by chance. (I never use it.) >> >> Empty-ness is just one of a billion things you might want to write a >> comparison for (and it's not even well defined). Having an operator for >> that >> would be crazy. Dealing with possibly undefined indexes/properties is a >> very >> common use case though which is why I think it deserves an operator. The >> "empty comparison" thing is a _separate issue_ and it can simply be dealt >> with by separating the assignment and comparison into two expressions... >> >> ~Hannes >> >> On 8 April 2011 15:19, Rune Kaagaard wrote: >> >> > Dear Internals >> > >> > I'm very happy that this is getting some attention again. Please allow >> > me to give my 2 cents too. The text below can also be seen nicely >> > formatted at https://gist.github.com/909711. >> > >> > ## Intro ## >> > >> > Isset and IsNotEmpty operators have for sure been a hot topic for >> several >> > years >> > now and in my opinion rightly so. The non-DRY style of: >> > >> > $my_array['my_long_boring_key'] = >> > !empty($my_array['my_long_boring_key']) >> > ? $my_array['my_long_boring_key'] : 'Default value'; >> > $my_array['my_long_boring_key'] = >> isset($my_array['my_long_boring_key']) >> > ? $my_array['my_long_boring_key'] : 'Default value'; >> > >> > is a true day-to-day hassle and addressing this annoyance would be a >> > big win for >> > the PHP community as a whole. As PHP has two keywords `isset` and >> `empty` >> > that >> > can check for a non existing variable without throwing errors I think >> there >> > should exist two assignment/ternary operators who mirror those. >> > >> > I have been thinking [1] about the same problem for my meta language >> Snow >> > and >> > also ended up using `??` as an isset operator. >> > >> > ## Proposal ## >> > I propose that two new operators `??` (IssetOperator) and `?!` >> > (NotEmptyOperator) are added. `??` mirrors `isset` and `?!` mirrors >> > `!empty`. >> > They are chainable ad nauseum but not with each other. >> > >> > They would work like this: >> > >> > ### Example 1 : Ternary shortcut ### >> > Old syntax: >> > $a = isset($b) ? $b : 42; >> > $a = !empty($b) ? $b : 42; >> > >> > New syntax: >> > $a = $b ?? 42; >> > $a = $b ?! 42; >> > >> > ### Example 2 : Direct assignment ### >> > Old syntax: >> > $arr['key'] = isset($arr['key']) ? $arr['key'] : 42; >> > $arr['key'] = !empty($arr['key']) ? $arr['key'] : 42; >> > >> > New syntax: >> > $arr['key'] ??= 42; >> > $arr['key'] ?!= 42; >> > >> > ### Example 3 : Works with statements too ### >> > Old syntax: >> > // a) >> > $tmp = get_stuff('foo'); >> > $a = isset($tmp) ? $tmp : 42; >> > >> > // b) >> > $tmp = get_stuff('foo'); >> > $a = !empty($tmp) ? $tmp : 42; >> > >> > New syntax: >> > // a) >> > $a = get_stuff('foo') ?? 42; >> > >> > // b) >> > $a = get_stuff('foo') ?! 42; >> > >> > ### Example 4 : Chaining ### >> > Old syntax [2]: >> > $a = false; >> > if (!empty($c) { >> > $a = $c; >> > } else { >> > $tmp = get_stuff(); >> > $a = !empty($tmp) ? $tmp : false; >> > } >> > if ($a === false) { >> > $a = !empty($c) ? $c : 42; >> > } >> > >> > New syntax: >> > $a = $c ?! get_stuff() ?! $b ?! 42; >> > >> > ### Example 5 : Illegal syntax ### >> > $a = $d ?? $c ?! $b ?? 42; // `??` and `?!` cannot be mixed. >> > >> > ## References ## >> > * [1]: http://code.google.com/p/php-snow/wiki/EmptyIssetOperators >> > * [2]: This could also be done by nesting ternary operators, but >> that >> > gets >> > even more unreadable I think. >> > >> > > --bcaec517a9aebe1e2f04a06a2535--