Cless Posted March 13, 2009 Share Posted March 13, 2009 I have a function which is used several times in my site, but it also needs access with a ton of variables, some of which change sometimes. Rather than going through and saying "global $var1, $var2, $var3" a hundred times, is there a way for me to make all variables usable in the function, preferably without using $GLOBALS? That way, I could do something such as: <?php //define variables $cows= 2; //declare moo function function moo() { //display content echo $cows; } //call moo function moo(); ?> Which, if worked, would display "2". Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/149275-solved-globals/ Share on other sites More sharing options...
WolfRage Posted March 13, 2009 Share Posted March 13, 2009 Does this script run as a user access the site or is it more of a cron job? Because then there are seperate approahces which are preferable under different circumstances. Quote Link to comment https://forums.phpfreaks.com/topic/149275-solved-globals/#findComment-783907 Share on other sites More sharing options...
Cless Posted March 13, 2009 Author Share Posted March 13, 2009 User access -- it is an online RPG. The functions I need are part of a battle system. Quote Link to comment https://forums.phpfreaks.com/topic/149275-solved-globals/#findComment-783910 Share on other sites More sharing options...
WolfRage Posted March 13, 2009 Share Posted March 13, 2009 For speed and efficiency I would use sessions. Then just insert the vars into $_SESSION. If you need help with this subject just let me know. Quote Link to comment https://forums.phpfreaks.com/topic/149275-solved-globals/#findComment-783913 Share on other sites More sharing options...
JonnoTheDev Posted March 13, 2009 Share Posted March 13, 2009 You should pass parameters into functions not set variables as global otherwise there is no requirement for a function. Quote Link to comment https://forums.phpfreaks.com/topic/149275-solved-globals/#findComment-783915 Share on other sites More sharing options...
Cless Posted March 13, 2009 Author Share Posted March 13, 2009 But wouldn't that be worse than globals? You'd have to define all of the sessions, then delete them all at the end (or the user will have hundreds of cookies on their computer [i think]). If I try to pass them through parameters, the declarations of the functions would be extremely confusing and tedious, like: test($var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1); Of course, they wouldn't all be "$var1", but it is pointless to go through this post and give them all a unique name, cause it's an example. Quote Link to comment https://forums.phpfreaks.com/topic/149275-solved-globals/#findComment-783916 Share on other sites More sharing options...
WolfRage Posted March 13, 2009 Share Posted March 13, 2009 One for a session there is only one cookie, everything else stays on the server, so no it would be extremly efficient and you just destroy the session. Also why not use an array, and hell you can even store the array in your session. Quote Link to comment https://forums.phpfreaks.com/topic/149275-solved-globals/#findComment-783920 Share on other sites More sharing options...
JonnoTheDev Posted March 13, 2009 Share Posted March 13, 2009 Put the variables into an array and pass them in within 1 parameter. Also why would you need so many variables within a function? Functions are for taking in data and returning a result. This could be a new value or an array of values. They are not designed and should hardly ever be used to modify the values of variables outside the scope of the function (for this job you would pass variables by reference). You will end up with unexpected results on your page without knowing the cause. Quote Link to comment https://forums.phpfreaks.com/topic/149275-solved-globals/#findComment-783922 Share on other sites More sharing options...
Cless Posted March 13, 2009 Author Share Posted March 13, 2009 But like I've said, I change variables quite often. Going through a huge array or several declarations can be very tedious, cause errors, etc. What about including the file, rather than using a function? So, instead of: <?php //define variables $var="blah"; //declare moo function function moo() { //display content echo $var; } //call moo function moo(); ?> I could do: <?php //define variables $moo="blah"; //include moo file include("moo.php"); ?> moo.php would be: <?php //display content echo $moo; ?> My worry is, though, that it would not be as efficient as using a function (like, it would increase filesize or something). Is that true? Quote Link to comment https://forums.phpfreaks.com/topic/149275-solved-globals/#findComment-783926 Share on other sites More sharing options...
WolfRage Posted March 13, 2009 Share Posted March 13, 2009 The more includes there will be a slight performance hit, but for the most part your overall parsed file size will be roughly the same. Still I would recommend using arrays and sessions, both will make your job a lot easier, particularly the sessions as you can pass all of that data quickly and efficiently as well as remembering it for use later on in the RPG. Quote Link to comment https://forums.phpfreaks.com/topic/149275-solved-globals/#findComment-783931 Share on other sites More sharing options...
Cless Posted March 13, 2009 Author Share Posted March 13, 2009 Alright, thanks for your help. :3 Quote Link to comment https://forums.phpfreaks.com/topic/149275-solved-globals/#findComment-783935 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.