http://us3.php.net/func_get_args http://us3.php.net/array
Enjoy. Eric Coleman Eric Coleman http://aplosmedia.com home: 412 399 1024 cell: 412 779 5176 On May 3, 2006, at 3:13 AM, D. Dante Lorenso wrote:
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=bI 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:<?php function ifsetor(&$var, $value) { return (isset($var)) ? $var : $value; } print ifsetor($x, "dante"); ?>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:<?phpfunction ifsetor(&$var1, &$var2, &$var3, &$var4, &$var5, &$var6, & $var7, &$var8, &$var9, &$var10) {if (isset($var1)) return $var1; if (isset($var2)) return $var2; if (isset($var3)) return $var3; if (isset($var4)) return $var4; if (isset($var5)) return $var5; if (isset($var6)) return $var6; if (isset($var7)) return $var7; if (isset($var8)) return $var8; if (isset($var9)) return $var9; if (isset($var10)) return $var10; return false; }print ifsetor($x["notset"]->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 2I 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:<?phpfunction ifsetor(&$var1=null, &$var2=null, &$var3=null, & $var4=null, &$var5=null, &$var6=null, &$var7=null, &$var8=null, & $var9=null, &$var10=null) {if (isset($var1)) return $var1; if (isset($var2)) return $var2; if (isset($var3)) return $var3; if (isset($var4)) return $var4; if (isset($var5)) return $var5; if (isset($var6)) return $var6; if (isset($var7)) return $var7; if (isset($var8)) return $var8; if (isset($var9)) return $var9; if (isset($var10)) return $var10; return false; }print ifsetor($x["notset"]->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'twork 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 Dantejohannes 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 listwhich would be quite useful to me and would simplify and clean up a lotof code out there: function coalesce(...) This works much like in the SQL version of the same. In SQL, thefunction returns the first non-null argument from an arbitrary list. Inour 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 bypasswarnings about undefined variables like 'empty' does. It might be niceto 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 userspacebecause the 'isset' and 'empty' checks need to be run before argumentscan 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-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
smime.p7s
Description: S/MIME cryptographic signature