[solved] function default values --help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

[solved] function default values --help

Post 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?
Last edited by anthony88guy on Sat Dec 02, 2006 3:36 pm, edited 1 time in total.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post 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
}
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

ahhh makes sense. Thank you.
Post Reply