Functions.

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
killroy
Forum Newbie
Posts: 3
Joined: Mon Dec 01, 2003 3:59 pm

Functions.

Post 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..
}
RoMeRz
Forum Newbie
Posts: 7
Joined: Mon Dec 01, 2003 3:19 pm
Location: Scotland

Post by RoMeRz »

i think the first one, inside the brackets. But i guess it depends on what your doing really.
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

I prefer the first way. I usually try to stay away from global variables because they can be insecure at times.
killroy
Forum Newbie
Posts: 3
Joined: Mon Dec 01, 2003 3:59 pm

Post 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.
killroy
Forum Newbie
Posts: 3
Joined: Mon Dec 01, 2003 3:59 pm

Post by killroy »

Thanks DuFF and RoMeRz
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
Post Reply