Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51884 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 6755 invoked from network); 14 Apr 2011 10:02:29 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Apr 2011 10:02:29 -0000 Authentication-Results: pb1.pair.com smtp.mail=rquadling@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rquadling@gmail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.170 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: rquadling@gmail.com X-Host-Fingerprint: 209.85.216.170 mail-qy0-f170.google.com Received: from [209.85.216.170] ([209.85.216.170:52414] helo=mail-qy0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A1/4A-44901-436C6AD4 for ; Thu, 14 Apr 2011 06:02:28 -0400 Received: by qyk32 with SMTP id 32so3224983qyk.8 for ; Thu, 14 Apr 2011 03:02:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:reply-to:in-reply-to:references :from:date:message-id:subject:to:cc:content-type :content-transfer-encoding; bh=JxssbVC+kyvY3E0eeatYBuNYYtoxQQGbnMgd2dWqDNA=; b=RCEek7N1B4pSuCmVG74JemKck2pEoxCVsq91EoYdNFM0C3FLPiaMunaiSNlZXtKr9h R4z0CTGy4aRfkLXG4fYsVQhTQ0+J+i8cCgdYMi/Ad1r0rNkLet2f6s1UuSHcErJYHYGV CskS6uHWr5BRzLJhLC14exrdtmoQfEMoAYcC8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:reply-to:in-reply-to:references:from:date:message-id :subject:to:cc:content-type:content-transfer-encoding; b=OQaAh+2htpgf3axW6IIykYLqPq0ufLbs60m1r4pSoEmbRbMDcz/JWMQVydcjaz5Sve hGXY017V7ZW72R7aISfLP/BuICBI5C3RSUVcx8VBDa7RQlaN73osBuRn5jbZBfya+a00 EvOV9uYuCMODSCvtWLvWsoYCvnoMVydNfrTTQ= Received: by 10.229.36.133 with SMTP id t5mr363677qcd.102.1302775345132; Thu, 14 Apr 2011 03:02:25 -0700 (PDT) MIME-Version: 1.0 Received: by 10.229.106.194 with HTTP; Thu, 14 Apr 2011 03:02:04 -0700 (PDT) Reply-To: RQuadling@googlemail.com 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> <718216446.20110408143441@cypressintegrated.com> <4DA0E71C.9030008@gmail.com> <4DA63ED8.4080402@yahoo.com.au> Date: Thu, 14 Apr 2011 11:02:04 +0100 Message-ID: To: Eloy Bote Falcon Cc: Ole Markus With , "internals@lists.php.net" , Ben Schmidt Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Implicit isset/isempty check on short-ternary operator From: rquadling@gmail.com (Richard Quadling) On 14 April 2011 09:59, Eloy Bote Falcon wrote: > What is the purpose of that generateHash function? It doesn't work in the > isset check. > > Anyway, you can do a simple $a =3D array('foo'); isset($a[$x][$y][$z]) wi= thout > notices at all unless any of $x $y or $z are not defined, you don't need = to > check the indexes one by one. > > 2011/4/14 Ole Markus With > >> On Thu, 14 Apr 2011 02:24:56 +0200, Ben Schmidt < >> mail_ben_schmidt@yahoo.com.au> wrote: >> >> =C2=A0I think these shortcuts could be really useful for array elements,= but >>>> for other variables I am really sceptical and I think they would do >>>> more harm than good. Generally I do not really see any reason why a >>>> variable should not be 'instanciated' before use. >>>> >>>> So >>>> +1 if this shortcut is implemented to only work for array elements. >>>> -1 for any other variable type. >>>> >>> >>> There are two issues here. >>> >>> =C2=A0 =C2=A01. Suppression of notice. I agree, it is best done only fo= r array >>> =C2=A0 =C2=A0keys. It's not hard to initialise a variable with $var=3Dn= ull at the >>> =C2=A0 =C2=A0beginning of a code block to avoid such a notice, and that= is the >>> =C2=A0 =C2=A0appropriate way to do it for variables. >>> >>> =C2=A0 =C2=A02. Offering a shortcut for the common idiom isset($x) ? $x= : $y in >>> =C2=A0 =C2=A0line with the DRY design principle. A notice would never b= e emitted >>> =C2=A0 =C2=A0here in any case. The problem is that this idiom is still = in wide use >>> =C2=A0 =C2=A0despite the shortcut ternary operator already provided, be= cause an >>> =C2=A0 =C2=A0isset() check is different to a boolean cast. >>> >>> Some thoughts: >>> >>> =C2=A0 =C2=A0- The actual intent of 2. is probably $x!=3D=3Dnull ? $x := $y i.e. it's >>> =C2=A0 =C2=A0 =C2=A0not about suppressing notices at all, but about off= ering a default >>> =C2=A0 =C2=A0 =C2=A0value, and the idiom quite probably only uses isset= () because it >>> =C2=A0 =C2=A0 =C2=A0predated null in the language. >>> >>> >> I have never felt the need for a shortcut in these cases. It would be >> interesting to see some code where this would be practical. >> >> >> =C2=A0 =C2=A0- If we view 2. in this way, the two problems are independe= nt, and it >>> =C2=A0 =C2=A0 =C2=A0seems to me it would be best to solve them independ= ently, rather >>> =C2=A0 =C2=A0 =C2=A0than with a single operator. >>> >>> So, I suggest: >>> >>> =C2=A0 =C2=A01. An array lookup mechanism that suppresses the notice fo= r undefined >>> =C2=A0 =C2=A0keys. It would work the same as regular array index lookup= s except >>> =C2=A0 =C2=A0that the notice for undefined keys (and only for undefined= keys) >>> =C2=A0 =C2=A0would not be generated (it would not just be hidden, but w= ould never >>> =C2=A0 =C2=A0be even generated). >>> >> >> This is what I feel PHP is missing. Particularly when it comes to >> multi-dimensional arrays. Because this feature is missing I tend to do >> something like >> >> function generateHash($x, $y, $z) >> { >> =C2=A0return "$x::$y::$z"; >> } >> >> if (isset($a[generateHash($x, $y, $z)]) { >> =C2=A0... >> } >> >> instead of >> >> if (isset($a[$x]) && isset($a[$x][$y]) && isset($a[$x][$y][$z])) { >> =C2=A0... >> >> } >> >> Arguing about syntax is then a second step. However, my views on this >>> are: >>> >>> >> I think it best to avoid discussing the actual syntax before agreeing on >> what we really need. >> >> -- >> Ole Markus With >> Systems Architect - Sportradar AS >> Developer - Gentoo Linux >> >> >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> > http://news.php.net/php.internals/51877 array_key_exists($key, $array) for arrays array_key_exists($varname, get_defined_vars()) for locally scoped variables= . No need to use @. isset() and empty() will not differentiate between an undefined variable or index and a variable or an array element which is assigned null. The E_NOTICE only ever gets fired for the undefined variable/key ... Notice: Undefined variable Notice: Undefined index I think it depends upon the developer's requirement. Are they attempting to determine the existence of a variable/index entry or are they attempting to determine if the variable/element is null. I always declare my variables. So, I don't want to use isset() as they will be an incorrect test. I use is_null(). Specifically testing the value. If I've made a mistake and NOT declared the variable (or made a typo), I want to know. I don't want to hide it with isset()/empty(). --=20 Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY