HoTDaWg Posted February 28, 2009 Share Posted February 28, 2009 hi there, i'm a bit confused by the difference between creating a function and referring to its variables by reference, or by just declaring the variable as global. What is the difference between both methods? thanks, HotDawg Link to comment https://forums.phpfreaks.com/topic/147311-solved-declaring-a-variable-as-global-in-a-function-or-calling-by-reference/ Share on other sites More sharing options...
premiso Posted February 28, 2009 Share Posted February 28, 2009 Global should only be used for certain situations. It is better to pass regular variables as parameters. One example of a global variable could be a database connection that you want to use in a function. Show the function you are working on and it can be explained a bit more. Link to comment https://forums.phpfreaks.com/topic/147311-solved-declaring-a-variable-as-global-in-a-function-or-calling-by-reference/#findComment-773258 Share on other sites More sharing options...
HoTDaWg Posted February 28, 2009 Author Share Posted February 28, 2009 oh thanks for the help i didnt have a specific case i am currently learning from a book and was wondering which would be best in which situation, or if i understood the concepts of calling by reference/globals correctly thanks for the help Link to comment https://forums.phpfreaks.com/topic/147311-solved-declaring-a-variable-as-global-in-a-function-or-calling-by-reference/#findComment-773259 Share on other sites More sharing options...
PFMaBiSmAd Posted February 28, 2009 Share Posted February 28, 2009 Functions should be passed any data they need as parameters and return any results. Passing a parameter by reference (a pointer to the actual data) so that the function operates on the original data should be avoided as well. If you have data and functions that are so closely related that you would end up considering using global, you should instead be using a class/OOP to embody that closely related data and function(s). Link to comment https://forums.phpfreaks.com/topic/147311-solved-declaring-a-variable-as-global-in-a-function-or-calling-by-reference/#findComment-773270 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.