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:
<?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:
<?php
function 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 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:
<?php
function 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'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