Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51848 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 80444 invoked from network); 9 Apr 2011 12:16:58 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 9 Apr 2011 12:16:58 -0000 Authentication-Results: pb1.pair.com header.from=rumi.kg@gmail.com; sender-id=pass; domainkeys=bad Authentication-Results: pb1.pair.com smtp.mail=rumi.kg@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.42 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: rumi.kg@gmail.com X-Host-Fingerprint: 209.85.212.42 mail-vw0-f42.google.com Received: from [209.85.212.42] ([209.85.212.42:41183] helo=mail-vw0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 7D/23-58719-93E40AD4 for ; Sat, 09 Apr 2011 08:16:57 -0400 Received: by vwl1 with SMTP id 1so3614791vwl.29 for ; Sat, 09 Apr 2011 05:16:54 -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:cc:content-type; bh=YYJrhB+BuRds/yLZgcNBwG6NxzW9h9dZuSFMR2WqWhc=; b=Sigy9Y9l56+PWVdEbjLV5/PX3Bhc2qYq0xW/ji65xKHilELhooXPOiDRN1ERIfCroU EiVxagMNWTycW86LTITwYxK/5izoyS5XoEV3Ghcay2HoZjCNtKH/fzRJ4VQbarvNvtCQ yhgPxeTp5EtdCYQFoNkLOsgJNkrioWGjoNxZA= 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 :cc:content-type; b=wGE+dWY7YXqH0PzzWNw1qlNwJAO5hRh95ah/V0t/P8LVay6jqZi3G7URNWpzxR2bn4 wFVVE0xO3XmZ4IkOoiO+8tcCseWBipNeYPrv3KiVnBptSQHA/Sq1Jf7Jmdd58/k+qIKu pss3tdwPyk8hOea3Nh8tkA/G2fB6ZMhSrLnkA= MIME-Version: 1.0 Received: by 10.52.76.195 with SMTP id m3mr1049236vdw.130.1302351412648; Sat, 09 Apr 2011 05:16:52 -0700 (PDT) Received: by 10.52.165.40 with HTTP; Sat, 9 Apr 2011 05:16:52 -0700 (PDT) In-Reply-To: <718216446.20110408143441@cypressintegrated.com> 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> <718216446.20110408143441@cypressintegrated.com> Date: Sat, 9 Apr 2011 14:16:52 +0200 Message-ID: To: Sanford Whiteman Cc: Matt Pelmear , internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] Implicit isset/isempty check on short-ternary operator From: rumi.kg@gmail.com (Rune Kaagaard) Hi guys Below is a discussion about some of the issues being raised. Again a nicely formatted version of the whole thing is here: https://gist.github.com/909711. ## Discussion ## ### About changing the beviour of the ternary shortcut ### @Jordi, others Some suggest adding an implicit `isset` around the value being tested in the ternary shortcut. This I think is less than optimal because: 1. It would be a huge BC break, because a boolean test and a isset test differs greatly. [3] 2. It would be a major WTF, because it would work different than the normal ternary operator that it is a shortcut of. Jordi suggests adding an implicit `!empty` around the value being tested in the ternary shortcut. This would not break BC but it still wouldn't improve usecases where the value tested for is allowed to be falsy ('', 0, "0", false, etc.): function render_articles($rows, $settings) { // Shows the last page per default. Zero based. $settings['page_num'] = $settings['page_num'] ?: count($rows) - 1; // ... } // Lets render_articles showing the first page. echo render_articles ($rows, array('page_num' => 0)); The obvious bug here is that since 0 is considered falsy, render_articles will show the last page instead of the first. The correct version is back in non-dry mode: $settings['page_num'] = isset($settings['page_num']) ? $settings['page_num'] : count($rows) - 1; Im writing the same stuff three times. With the `??` operator it would be: $settings['page_num'] ??= count($rows) - 1; ### Why we need both an Isset and IsNotEmpty operator ### @Hannes, Ben What's considered falsy in PHP is _very_ dynamic. Depending on context the following can easily be considered falsy: '', null, undefined, array(), false, 0 Therefore we need to be very precise when we are making decisions based on a variable or statements value. This is where `isset` and `!empty` compliments each other very well. Sometimes i want to use a default value when falsy means "this variable is not defined". Other times I want to assign a default value when falsy means "this variable is not an empty string". Sometimes the behaviour we want will change while we are working on the code. Therefore I strongly believe that not having both operators would severely break the symmetry and force the end user to make tedious and non-dry refactorings just because he needs a function to - under certain conditions - return false or vice versa. ## References ## * [3]: http://php.net/manual/en/types.comparisons.php