Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:23101 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 73388 invoked by uid 1010); 3 May 2006 05:57:00 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 73373 invoked from network); 3 May 2006 05:57:00 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 May 2006 05:57:00 -0000 X-PHP-List-Original-Sender: dante@lorenso.com X-Host-Fingerprint: 69.56.234.131 unknown Linux 2.5 (sometimes 2.4) (4) Received: from ([69.56.234.131:44019] helo=dante.domain.com) by pb1.pair.com (ecelerity 2.0 beta r(6323M)) with SMTP id 61/4E-31513-B2648544 for ; Wed, 03 May 2006 01:57:00 -0400 Received: from [127.0.0.1] (c-67-177-79-15.hsd1.tx.comcast.net [67.177.79.15]) by dante.domain.com (Postfix) with ESMTP id 3E70626025D for ; Wed, 3 May 2006 00:56:53 -0500 (CDT) Message-ID: <44584619.60400@lorenso.com> Date: Wed, 03 May 2006 00:56:41 -0500 Reply-To: dante@lorenso.com Organization: LarkSpark Corporation User-Agent: Thunderbird 1.5.0.2 (Windows/20060308) MIME-Version: 1.0 To: internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Seeking 'coalesce' php internal function From: dante@lorenso.com ("D. Dante Lorenso") 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