Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:23103 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 78020 invoked by uid 1010); 3 May 2006 06:16:45 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 78004 invoked from network); 3 May 2006 06:16:44 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 May 2006 06:16:44 -0000 X-PHP-List-Original-Sender: johannes@php.net X-Host-Fingerprint: 212.227.48.61 mayflowerholding.com Linux 2.5 (sometimes 2.4) (4) Received: from ([212.227.48.61:40520] helo=s15175637.rootmaster.info) by pb1.pair.com (ecelerity 2.0 beta r(6323M)) with SMTP id E1/CE-31513-BCA48544 for ; Wed, 03 May 2006 02:16:44 -0400 Received: (qmail 698 invoked by uid 2520); 3 May 2006 08:16:16 +0200 Received: from 82.135.15.184 by s15175637 (envelope-from , uid 2020) with qmail-scanner-1.25st (uvscan: v4.4.00/v4751. spamassassin: 3.0.3. perlscan: 1.25st. Clear:RC:0(82.135.15.184):SA:0(-1.5/7.0):. Processed in 1.756129 secs); 03 May 2006 06:16:16 -0000 X-Spam-Status: No, hits=-1.5 required=7.0 Received: from ppp-82-135-15-184.mnet-online.de (HELO ?192.168.1.102?) (82.135.15.184) by mayflowerholding.com with (DHE-RSA-AES256-SHA encrypted) SMTP; 3 May 2006 08:16:14 +0200 To: internals@lists.php.net, dante@lorenso.com Date: Wed, 3 May 2006 08:15:34 +0200 User-Agent: KMail/1.9.1 References: <44584619.60400@lorenso.com> In-Reply-To: <44584619.60400@lorenso.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-ID: <200605030815.34801.johannes@php.net> Subject: Re: [PHP-DEV] Seeking 'coalesce' php internal function From: johannes@php.net (Johannes Schlueter) Hi, please search the archives for "ifsetor". johannes On Wednesday 03 May 2006 07:56, D. Dante Lorenso wrote: > All, > > I'm sure this has been asked somewhere, but since I see requests for > features for 5.2 or 6.0, I'd like to add a "simple" item to the list > which would be quite useful to me and would simplify and clean up a lot > of code out there: > > function coalesce(...) > > This works much like in the SQL version of the same. In SQL, the > function returns the first non-null argument from an arbitrary list. In > our use, it should return the first non-empty value from the list: > > Example: > > $x = "dante"; > $y = ""; > $z = ""; > > $value = coalesce($z, $y, $x); // $value = "dante" > > This function would ideally be built into the language and bypass > warnings about undefined variables like 'empty' does. It might be nice > to have several varieties of this function: > > * return first parameter where empty() is FALSE > * return first parameter where isset() is TRUE > > I don't think something like this can NOT be written in userspace > because the 'isset' and 'empty' checks need to be run before arguments > can be passed to a user function or warnings will start flying. A > function like this simplifies code which used to look like this: > > if (!empty($_POST["postkey"])) { > $value = $_POST["postkey"]; > } > elseif (!empty($_GET["getkey"])) { > $value = $_POST["getkey"]; > } > elseif (!empty($default_value)) { > $value = $default_value; > } > else { > $value = "hard coded value"; > } > > Into this: > > $value = coalesce($_POST["postkey"], $_GET["getkey"], > $default_value, "hard coded value"); > > Can this be built and included? > > Dante