Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51871 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 44291 invoked from network); 11 Apr 2011 19:24:33 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Apr 2011 19:24:33 -0000 Authentication-Results: pb1.pair.com smtp.mail=etienne@immomigsa.ch; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=etienne@immomigsa.ch; sender-id=unknown Received-SPF: error (pb1.pair.com: domain immomigsa.ch from 94.103.100.39 cause and error) X-PHP-List-Original-Sender: etienne@immomigsa.ch X-Host-Fingerprint: 94.103.100.39 mailer.immomigsa.ch Linux 2.6 Received: from [94.103.100.39] ([94.103.100.39:59932] helo=mailer.immomigsa.ch) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 51/03-24294-17553AD4 for ; Mon, 11 Apr 2011 15:24:33 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by mailer.immomigsa.ch (Postfix) with ESMTP id 0C6544B62697; Mon, 11 Apr 2011 21:24:30 +0200 (CEST) X-Virus-Scanned: amavisd-new at immomigsa.ch Received: from mailer.immomigsa.ch ([127.0.0.1]) by localhost (mailer.immomigsa.ch [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id jfkNHsYFXgco; Mon, 11 Apr 2011 21:24:29 +0200 (CEST) Received: from crousti (tsf-wpa-2-5066.epfl.ch [128.178.245.66]) by mailer.immomigsa.ch (Postfix) with ESMTPSA id 9F1264B624FA; Mon, 11 Apr 2011 21:24:28 +0200 (CEST) Received: by crousti (sSMTP sendmail emulation); Mon, 11 Apr 2011 21:23:07 +0200 Date: Mon, 11 Apr 2011 21:23:07 +0200 To: Todd Ruth Cc: Internals Mailing List Message-ID: <20110411192307.GP7113@crousti> References: <4DA26602.6080303@lorenso.com> <20110411133657.GK7113@crousti> <4DA3468E.8000509@sugarcrm.com> <1302548895.15421.18.camel@inspiron> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1302548895.15421.18.camel@inspiron> User-Agent: Mutt/1.5.20 (2009-06-14) Subject: Re: [PHP-DEV] Avoiding "Undefined index" notices From: etienne@immomigsa.ch (Etienne Kneuss) On Apr 11 12:08:15, 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 > threads, I thought I'd show how I feel I've been encourage by > php to code: > > $x = array(); > $y = 'something'; > $temp =& $x[$y]; > $temp++; > unset($temp); > ?> > > 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. > > Here is an application to the defaults for configurations thread: > > $config = array(); > $param = 'param1'; > $temp =& $config[$param]; > $temp = $temp ?: 'default'; > unset($temp); > var_dump($config); > ?> > > It isn't beautiful, but it avoids writing "$config[$param]" > more than once and it avoids the notices. I believe that using + for configuration settings makes much more sense: $defaults = array('foo' => 'bar', 'gee' => 'plop'); $conf += $defaults; and that's it. You no longer have to worry about 'foo' or 'gee' being defined, whether you use them or not: $conf is fully defined for every possible configuration settings. Now you could of course use the same mechanism for filling $_GET and $_POST, but I usually prefer leaving $_GET and $_POST untouched, so that it still reflects what the user actually sent, so even the technique with references would not work for me. Best, > > - Todd > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >