Jump to content

Archived

This topic is now archived and is closed to further replies.

GingerRobot

functions

Recommended Posts

Is there a way to make a variable available inside a function that was created outside of it, without passing it into the function as a parameter?

Ive got some variables from a database, and i want to be able to use them inside a function.

Thanks in advance.

Share this post


Link to post
Share on other sites
Its not the best of ideas, but you could use the global keyword.

[code=php:0]
$var = "bar";
function foo();
  global $var;
  echo $var;
}
foo();
[/code]

Share this post


Link to post
Share on other sites
In order to use a variables that were created outside of the function you'll have to define it as global within the function, which thorpe has provided an example of.

Share this post


Link to post
Share on other sites
Thanks, i thought you used the global keyword to make variables inside the function accessible ouside. Thanks again.

Share this post


Link to post
Share on other sites
I was told from my teacher that to exstend a function you should make a class so in my teachers option the above code is not good code pratice is that correct.

cheers.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.