Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51869 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 41444 invoked from network); 11 Apr 2011 19:14:48 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Apr 2011 19:14:48 -0000 Authentication-Results: pb1.pair.com smtp.mail=shadda@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=shadda@gmail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.170 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: shadda@gmail.com X-Host-Fingerprint: 209.85.214.170 mail-iw0-f170.google.com Received: from [209.85.214.170] ([209.85.214.170:49049] helo=mail-iw0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F9/62-24294-72353AD4 for ; Mon, 11 Apr 2011 15:14:48 -0400 Received: by iwn3 with SMTP id 3so7263016iwn.29 for ; Mon, 11 Apr 2011 12:14:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:subject:mime-version:content-type:from :in-reply-to:date:cc:content-transfer-encoding:message-id:references :to:x-mailer; bh=WrMApMAVMyDcAXFUZ8siA5Etp1d0pa694UiTzieOeNc=; b=w/dof/fmNZVnZOueP/5j6jgI9x4FVuX8rE+NaE0lG2k9iX/Jc9y7jMi9I4vkIz5q1Z Zm+SC+Dw+U1OYCLa6TI0IszlQb2Vxc2VvC3NsIOO5XRG99dwgDDoXcQJedhJeZXDiioj T7sBpqZnTTY/r5oFgxYRG7da+g/K0k7WF3oAc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=subject:mime-version:content-type:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to:x-mailer; b=EJMJ6I0a9HnKjFF+Pr7wyRtqiKuTG+wdrKdCjYfYqMfZyisG+/7+SccjLbxJMiAeRD DEKtPwK8iSF/yVX1nQ3hvd7S2KIqI1tqvm3N82N4Xn5vK7dWSHrIpqhtayHZSSqPKjUE JyFpk/E+wSOOGymdsp8xuEYwBuZ6YZHSicCvI= Received: by 10.43.54.83 with SMTP id vt19mr294820icb.155.1302549285250; Mon, 11 Apr 2011 12:14:45 -0700 (PDT) Received: from mattw-mbp.adknowledge.com ([204.137.29.243]) by mx.google.com with ESMTPS id d9sm4392231ibb.53.2011.04.11.12.14.42 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 11 Apr 2011 12:14:43 -0700 (PDT) Mime-Version: 1.0 (Apple Message framework v1084) Content-Type: text/plain; charset=us-ascii In-Reply-To: <1302548895.15421.18.camel@inspiron> Date: Mon, 11 Apr 2011 14:14:40 -0500 Cc: Internals Mailing List Content-Transfer-Encoding: quoted-printable Message-ID: References: <4DA26602.6080303@lorenso.com> <20110411133657.GK7113@crousti> <4DA3468E.8000509@sugarcrm.com> <1302548895.15421.18.camel@inspiron> To: truth@proposaltech.com X-Mailer: Apple Mail (2.1084) Subject: Re: [PHP-DEV] Avoiding "Undefined index" notices From: shadda@gmail.com (Matt Wilson) Something I've been doing for a long time is similar, but in my opinion = a much better practice. function coalesce(&$val, $ifnull =3D false) { return empty($val) ? $ifnull : $val; } Add whatever restrictive logic you wish here, if empty() isn't good = enough for your purposes.=20 $_GET['mightnotexist'] =3D coalesce($_GET['mightnotexist'], 'default'); Personally, I think if you're writing your code where a variable's = existence is ambiguous, you *should* be seeing notices. On Apr 11, 2011, at 2:08 PM, Todd Ruth wrote: > I'm not arguing whether the following code fragment is good > or bad (it's certainly less than ideal), but given the recent=20 > threads, I thought I'd show how I feel I've been encourage by=20 > php to code: >=20 > $x =3D array(); > $y =3D 'something'; > $temp =3D& $x[$y]; > $temp++; > unset($temp); > ?> >=20 > I'm not sure where (if anywhere) that technique is documented > or even if it should be documented, but if you want to avoid > "Undefined index" notices, that's one of the more terse > approaches. The relative brevity is more obvious when the > variable names are long and $temp is re-linked several times > before unsetting. It's probably less clear than alternatives > unless you see it often. >=20 > Here is an application to the defaults for configurations thread: >=20 > $config =3D array(); > $param =3D 'param1'; > $temp =3D& $config[$param]; > $temp =3D $temp ?: 'default'; > unset($temp); > var_dump($config); > ?> >=20 > It isn't beautiful, but it avoids writing "$config[$param]" > more than once and it avoids the notices. >=20 > - Todd >=20 >=20 > --=20 > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >=20