Page 1 of 1

[solved] function default values --help

Posted: Sat Dec 02, 2006 10:13 am
by anthony88guy
After a few months of neglecting PHP, I decided to get back into it. Of course I forgot a few things...

Parse error: parse error, unexpected T_VARIABLE in ... on line 11

Code: Select all

function insertFeedback($name, $type, $email, $text, $ip = $_SERVER['REMOTE_ADDR'], $time = time()) //line 11
{
blah blah blah
}
Am I setting the default values correctly?

Posted: Sat Dec 02, 2006 11:32 am
by GeertDD
The default argument value must be a constant expression, not (for example) a variable, a class member or a function call.

http://www.php.net/manual/en/functions.arguments.php
To implement the same functionality in a valid way, I'd do it like this:

Code: Select all

function insertFeedback($name, $type, $email, $text, $ip = '', $time = '') {

 if ($ip == '') $ip = $_SERVER['REMOTE_ADDR'];
 if ($time == '') $time = time();

 // the rest of the function
}

Posted: Sat Dec 02, 2006 2:23 pm
by anthony88guy
ahhh makes sense. Thank you.