jaymc Posted March 26, 2009 Share Posted March 26, 2009 How can I use external variables within a function. At the moment I am having to use global $varname; for every single var I need to use within that function e.g. function block_build($id) { global $session; global $gallery_user; global $link; global $gallery_name; global $front_image; global $rounded_top; global $rounded_bottom; } Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted March 26, 2009 Share Posted March 26, 2009 That's exactly how you do it. Quote Link to comment Share on other sites More sharing options...
Mchl Posted March 26, 2009 Share Posted March 26, 2009 Or pass these variables as arguments to a function. Quote Link to comment Share on other sites More sharing options...
jaymc Posted March 28, 2009 Author Share Posted March 28, 2009 It seems very tedius especially when I have 5+ variables I need to use Are you saying this is the correct and only ways to go about this? Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted March 28, 2009 Share Posted March 28, 2009 You could pass an array but you're still doing about the same amount of work. This isn't even close to tedious when you get into the more advanced stuff. If you don't want to do this, then I'd suggest getting another hobby. Quote Link to comment Share on other sites More sharing options...
Kieran Menor Posted March 28, 2009 Share Posted March 28, 2009 If it makes it any easier, you can specify them all on a single line, i.e.: global $session, $gallery_user, $link, $gallery_name, $front_image, $rounded_top, $rounded_bottom; Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted March 28, 2009 Share Posted March 28, 2009 You could encapsulate whatever functionality this is inside a class with properties. Then you don't have to pass anything around and each function has access to them. Quote Link to comment Share on other sites More sharing options...
Kieran Menor Posted March 28, 2009 Share Posted March 28, 2009 You could encapsulate whatever functionality this is inside a class with properties. Then you don't have to pass anything around and each function has access to them. Judging from his question, I am not sure he is ready to take on OOP, but sure, this is also a possibility. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 28, 2009 Share Posted March 28, 2009 Don't use the global keyword to pass variables into functions - it prevents writing recursive functions, it prevents using default values for optional parameters, and it makes your code hard to debug a short time after you have written it because you won't remember which variables you need to setup before calling any function (for those of use that have more than one or two user written functions to keep track of.) Do use parameters to pass variables into functions - is allows writing recursive functions, it allows default values for optional parameters, and it makes your code easier to debug because you can see by just reading the function call what values are being used and if you named them appropriately, you don't need to continually go back and read the function definition to figure out what they mean. If you have some variables that are so closely related to a function that the global keyword makes sense, you should probably be using a class instead of functions. Quote Link to comment Share on other sites More sharing options...
jaymc Posted March 30, 2009 Author Share Posted March 30, 2009 Cheers guys Quote Link to comment Share on other sites More sharing options...
corbin Posted March 30, 2009 Share Posted March 30, 2009 If you do decide to go with globals (boo, hiss!), you can combine multiple global statements: global $var1, $var2; Quote Link to comment 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.