Page 1 of 1

Functions.

Posted: Mon Dec 01, 2003 3:59 pm
by killroy
Hi all,

First time poster, go easy :)

Quick one for you all. When writing a functions would it be better to send the $variables from an HTML form through the function or as global.

I hope this makes sense, as I am learning...

eg:

Code: Select all

function foo($apples,$pears,$lemons) {

  code..
}
or

Code: Select all

function foo() {

  global $apples,$pears,$lemons;
  code..
}

Posted: Mon Dec 01, 2003 4:03 pm
by RoMeRz
i think the first one, inside the brackets. But i guess it depends on what your doing really.

Posted: Mon Dec 01, 2003 4:13 pm
by DuFF
I prefer the first way. I usually try to stay away from global variables because they can be insecure at times.

Posted: Mon Dec 01, 2003 4:17 pm
by killroy
Well I have a registration form, and I made a function that will add all the values into the specified table. So it was a case of saying something like this.

Code: Select all

if ($submit) {

  foo();
}
of

Code: Select all

if ($submit) {

  foo($apples,$pears,$lemons);
}
All I really need is confirmation on which is the 'better' method. if there is no problem doing it either way, then I will useto the foo($apples,$pears,$lemons); option.

Thanks.

Posted: Mon Dec 01, 2003 4:17 pm
by killroy
Thanks DuFF and RoMeRz

Posted: Tue Dec 02, 2003 2:10 pm
by McGruff
A general principle of application design is to minimise variable scope as much as is reasonable. That way, it's much easier to track down what's causing a problem if sopmething goes wrong.

Avoid globals like the plague.