Jump to content


Photo

functions


  • Please log in to reply
4 replies to this topic

#1 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 17 July 2006 - 03:06 PM

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.

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 17 July 2006 - 03:09 PM

Its not the best of ideas, but you could use the global keyword.

$var = "bar";
function foo();
  global $var;
  echo $var;
}
foo();


#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 17 July 2006 - 03:12 PM

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.

#4 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 17 July 2006 - 03:14 PM

Thanks, i thought you used the global keyword to make variables inside the function accessible ouside. Thanks again.

#5 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 17 July 2006 - 03:30 PM

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.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users