batfastad wrote:I guess it's not possible to set the default of an optional argument to a variable value. Is there a way to do what I'm looking for?
Or do I have to explicitly tell the function what the default value is?
Correct, you can't set a default value to a variable because the script is compiled (including setting function defaults) before it is executed, and the variable is set during execution.
You can, however, set defaults to constants
Code: Select all
define ('const1','thisValue');
define ('const2','thatValue');
define ('const3','theOtherValue');
function my_function($arg1=const1, $arg2=const2, $arg3=const3) {
//do stuff
}
An ugly alternative:
Code: Select all
function my_function($arg1=null, $arg2=null, $arg3=null) {
global $var1, $var2, $var3;
if (is_null($arg1)) { $arg1 = $var1; }
if (is_null($arg2)) { $arg2 = $var2; }
if (is_null($arg3)) { $arg3 = $var3; }
//do stuff
}