Jakez Posted March 23, 2010 Share Posted March 23, 2010 Ok let's say I am loading settings from MySQL into varibles like so: $result=mysql_query("SELECT * FROM table WHERE id='1' LIMIT 1"); while($row=mysql_fetch_array($result)) { $path=$row['path']; $etc=$row['etc']; } Then I have a function: function func_name() { global $path, $etc; print $path.$etc; } Is there a way to "pre" global these variables so that I can use them in all my functions without having to global them inside each function? I have a lot of variables loaded in the settings and I'd rather not have to global them in every function. Link to comment https://forums.phpfreaks.com/topic/196188-how-to-use-variables-outside-of-a-function-without-globaling-each-one/ Share on other sites More sharing options...
nafetski Posted March 23, 2010 Share Posted March 23, 2010 Declare them outside of the function, and they'll be global Link to comment https://forums.phpfreaks.com/topic/196188-how-to-use-variables-outside-of-a-function-without-globaling-each-one/#findComment-1030279 Share on other sites More sharing options...
Jakez Posted March 23, 2010 Author Share Posted March 23, 2010 Declare them outside of the function, and they'll be global I think I just learned something while searching Google. Do I just use $GLOBALS[path] and $GLOBALS[etc] inside the function? Link to comment https://forums.phpfreaks.com/topic/196188-how-to-use-variables-outside-of-a-function-without-globaling-each-one/#findComment-1030280 Share on other sites More sharing options...
trq Posted March 23, 2010 Share Posted March 23, 2010 Declare them outside of the function, and they'll be global No they won't. To the OP. Globals should be avoided, pass your arguments into your functions and return data from them. Link to comment https://forums.phpfreaks.com/topic/196188-how-to-use-variables-outside-of-a-function-without-globaling-each-one/#findComment-1030355 Share on other sites More sharing options...
dstar101 Posted March 23, 2010 Share Posted March 23, 2010 Add the code in new file and use require_once to access them.You still need to use global statement Link to comment https://forums.phpfreaks.com/topic/196188-how-to-use-variables-outside-of-a-function-without-globaling-each-one/#findComment-1030356 Share on other sites More sharing options...
trq Posted March 23, 2010 Share Posted March 23, 2010 Add the code in new file and use require_once to access them.You still need to use global statement That does nothing to answer the op's question. Link to comment https://forums.phpfreaks.com/topic/196188-how-to-use-variables-outside-of-a-function-without-globaling-each-one/#findComment-1030357 Share on other sites More sharing options...
ignace Posted March 23, 2010 Share Posted March 23, 2010 $result = mysql_query('SELECT path, etc FROM table WHERE id=1 LIMIT 1'); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { func_name($row['path'], $row['etc']); } Link to comment https://forums.phpfreaks.com/topic/196188-how-to-use-variables-outside-of-a-function-without-globaling-each-one/#findComment-1030399 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.