Re: The default values of variables in a function
| From: | Steve Edberg | Date: | Mon, 25 Sep 2000 18:12:58 +0000 |
| Subject: | Re: The default values of variables in a function | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-17358@lists.php.net to get a copy of this message | ||
At 10:03 PM -0400 9/21/00, Matt wrote:
I'm trying to set a default value for a variable in a php function. See function below. With the following, it gives me an error because I'm using a variable: function RunQuery($sql,$datasource=$app_mySQL_DSN) if I change it to this, it works: function RunQuery($sql,$datasource="theDSN") Any ideas as to my this is happening or if there is a work around?Well, at least in the first form, that is illegal syntax (in PHP3...don't know about PHP4). The workaround is simple: function RunQuery($sql,$datasource=-1) { global $app_mySQL_DSN; if ($datasource == -1) {$datasource = $app_mySQL_DSN; } ... } Two things to note: (1) the key here is to set $datasource to some invalid value as the default; if -1 is a valid value here [in this context, it doesn't look like it], then you'd have to pick something else. (2) I'm assuming here that $app_mySQL_DSN was set it the main program; thus, the global declaration. If this var is set within the subroutine, then you can just replace the $datasource = $app_mySQL_DSN; statement with the appropriate declaration. -steve +--- "They've got a cherry pie there, that'll kill ya" ------------------+
| Steve Edberg University of California, Davis | | sbedberg@ucdavis.edu Computer Consultant | | http://aesric.ucdavis.edu/ http://pgfsun.ucdavis.edu/ |+-------------------------------------- FBI Special Agent Dale Cooper ---+