Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:23111 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 3514 invoked by uid 1010); 3 May 2006 07:13:52 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 3497 invoked from network); 3 May 2006 07:13:52 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 May 2006 07:13:52 -0000 X-PHP-List-Original-Sender: dante@vocalspace.com X-Host-Fingerprint: 69.56.193.72 fox02.stravio.com Linux 2.5 (sometimes 2.4) (4) Received: from ([69.56.193.72:43663] helo=fox02.stravio.com) by pb1.pair.com (ecelerity 2.0 beta r(6323M)) with SMTP id 87/A2-31513-F2858544 for ; Wed, 03 May 2006 03:13:51 -0400 Received: from [127.0.0.1] (c-67-177-79-15.hsd1.tx.comcast.net [67.177.79.15]) by fox02.stravio.com (Postfix) with ESMTP id DBD9126C31A; Wed, 3 May 2006 02:13:47 -0500 (CDT) Message-ID: <44585828.1030603@vocalspace.com> Date: Wed, 03 May 2006 02:13:44 -0500 User-Agent: Thunderbird 1.5.0.2 (Windows/20060308) MIME-Version: 1.0 To: Johannes Schlueter Cc: internals@lists.php.net, dante@lorenso.com References: <44584619.60400@lorenso.com> <200605030815.34801.johannes@php.net> In-Reply-To: <200605030815.34801.johannes@php.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Seeking 'coalesce' php internal function From: dante@vocalspace.com ("D. Dante Lorenso") Johannes Schlueter wrote: > please search the archives for "ifsetor". > I have completed this search and find: http://marc.theaimsgroup.com/?r=1&w=2&q=b&l=php-dev&s=coalesce http://marc.theaimsgroup.com/?l=php-dev&w=2&r=1&s=ifsetor&q=b I am using PHP 5.1.2 currently and thought using pass-by-reference was frowned upon. Assuming a mistaken assumption I move forward and see the following code which was presented to the list: This works as I'd like in my dev environment, however, this does not solve the problem of allowing variable number of parameters (variables and values). So, I've whipped up this 10 variable version: notset, $y["notset"]->notset, $z->notset, "dante"); ?> PHP Fatal error: Only variables can be passed by reference in .../ifsetor.php on line 15 PHP Warning: Missing argument 5 for ifsetor(), called in .../ifsetor.php on line 15 and defined in .../ifsetor.php on line 2 PHP Warning: Missing argument 6 for ifsetor(), called in .../ifsetor.php on line 15 and defined in .../ifsetor.php on line 2 PHP Warning: Missing argument 7 for ifsetor(), called in .../ifsetor.php on line 15 and defined in .../ifsetor.php on line 2 PHP Warning: Missing argument 8 for ifsetor(), called in .../ifsetor.php on line 15 and defined in .../ifsetor.php on line 2 PHP Warning: Missing argument 9 for ifsetor(), called in .../ifsetor.php on line 15 and defined in .../ifsetor.php on line 2 PHP Warning: Missing argument 10 for ifsetor(), called in .../ifsetor.php on line 15 and defined in .../ifsetor.php on line 2 I can work around passing values by reference by calling the function like this: print ifsetor($x["notset"]->notset, $y["notset"]->notset, $z->notset, $temp = "dante"); But you have to admit that's getting ugly and still, there are undefined parameters declared ... so then I tried it by assigning default values: notset, $y["notset"]->notset, $z->notset, $temp = "dante")."\n"; print_r($x); ?> Now, $x, $y, and $z ARE set ... dante Array ( [notset] => stdClass Object ( [notset] => ) ) This is a nasty side-effect and can not be accepted. I did not want to set these variables, just check their existance. I am willing to go away and concede that this should be done in userspace IF you can show me that it is in fact possible. The 2 argument ifsetor is trivial in userspace, yes, but the variable case is more useful in a variety of situations and provides the most benefit to simplify code. I did not find the answer in the archives. Can you point me in the right direction? I need to be able to do all of the following: * variable number of parameters * test 'isset' or 'empty' * testing variables and non-variable values (pass-by-reference won't work on values) * not have side-effect of defining values which are not already set * not trigger notices or warnings * returns value of first proper match Dante > 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 >> > >