Forum Moderators: coopster

Message Too Old, No Replies

$ SERVER var as default argument value in function?

why won't this work

         

pixeltierra

5:46 pm on Aug 23, 2006 (gmt 0)

10+ Year Member



As far as I can tell, this won't work:

function http_redirect ($location = $_SERVER['HTTP_REFERER'], $msg = "Processing", $delay = 0) {

but this will:

function http_redirect ($location, $msg = "Processing", $delay = 0) {

What's up?

jatar_k

6:00 pm on Aug 23, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hmm, I was reading this looking to see iof they specifically mention something that would provide the answer

Default argument values [php.net]

I did see this

The default value must be a constant expression, not (for example) a variable, a class member or a function call.

do you get errors of any kind?
what exactly happens when you try the first example?

coopster

6:23 pm on Aug 23, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



That is exactly it, jatar_k.

$_SERVER['HTTP_REFERER'] is a variable. Can't do that, pixeltierra. As a workaround, what you can do is pass in something else, like maybe a boolean false value. If the variable passed is exactly equal to false you can set it to the value of the superglobal ...

function http_redirect ($location = false, $msg = "Processing", $delay = 0) 
{
if ($location === false) {
$location = $_SERVER['HTTP_REFERER'];
}
...

pixeltierra

6:30 pm on Aug 23, 2006 (gmt 0)

10+ Year Member



Thanks guys. I'll make do.