GingerRobot Posted July 17, 2006 Share Posted July 17, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14857-functions/ Share on other sites More sharing options...
trq Posted July 17, 2006 Share Posted July 17, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/14857-functions/#findComment-59436 Share on other sites More sharing options...
wildteen88 Posted July 17, 2006 Share Posted July 17, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14857-functions/#findComment-59440 Share on other sites More sharing options...
GingerRobot Posted July 17, 2006 Author Share Posted July 17, 2006 Thanks, i thought you used the global keyword to make variables inside the function accessible ouside. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/14857-functions/#findComment-59443 Share on other sites More sharing options...
redarrow Posted July 17, 2006 Share Posted July 17, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14857-functions/#findComment-59453 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.