i have written a function all over my site which is used to display an error.

$input= "error msg";
error($input);

then that function will display the error msg and exit();. The problem is, i need to get a new variable into the function but i don't want to have to go through ever error(); function and change it to error($new, $error);

So is there anyway else to get a variable(that has already been set mind you) into the function.

The variable i'm trying to get in is the users ID, which is set at the top of the page.

There are two things you can do. You can use the func_num_args() (http://www.php.net/manual/en/function.func-num-args.php) function in your error function to determine how many arguments have been passed to the function, and then the func_get_args() (http://www.php.net/manual/en/function.func-get-args.php) function to retrieve those arguments. This frees you from requiring a specific number of arguments to be passed to your error function.

The second thing you can do is assign a default value to your new second argument.

function error($msg, $flag = NULL) {}

This technique makes the second argument optional, as it will default to NULL if it's missing.

(EDIT) Brandoe85 beat me to it; but a note about his code snippet; You must default the second argument rather than the first, as the way he's written it will still require a second argument (and thus require you to change all of your code).

ole90

10-27-2007, 10:45 PM

Hmm well, i want to do function error($userid = $userid2, $input) {

}

but that gives me an error. Any other way?

Fumigator

10-27-2007, 11:22 PM

You can't default to a variable. You can do global $userid2; but that's sloppy. (It will work for you though)

ole90

10-28-2007, 01:18 AM

$userid2 is already global. But it still won't work inside my function. I even echo'ed it outside the function to see if it contained a value and it did. But inside the function it always comes up as 0...

Inigoesdr

10-28-2007, 03:26 AM

Post your code. If it's global and has a different value then it's being overwritten.