Hall of Famer Posted September 4, 2011 Share Posted September 4, 2011 Well I heard that registering $GLOBALS is a bad practice in general since their values can be changed by anyone at anytime. However, the usage of $GLOBALS does simplify the script considerably at times when a certain column in a table needs to be retrieved repeatedly. A good example is user's money data stored in table prefix_users as shown below: $result = mysql_query( "SELECT * FROM {$prefix}users WHERE uid = '$uid'"); $GLOBALS['usersettings'] = mysql_fetch_array($result); $GLOBALS['money'] = $GLOBALS['usersettings']['money']; If the above code is included in a function file, it will be possible to simply use $GLOBALS['money'] to retrieve user's money data without having to write lines of mysql commands everytime. So I was wondering, is there another way to retrieve database info from a certain column easily but not to register $GLOBALS? Just curious. Quote Link to comment https://forums.phpfreaks.com/topic/246425-about-registering-globals/ Share on other sites More sharing options...
jcbones Posted September 4, 2011 Share Posted September 4, 2011 There is absolutely no reason to use globals. Using a standard variable, and passing arguments to functions is how it should be done. Passing a global to a function defeats the purpose of using a function, as you have just tied a specific variable to a function. Dropping that function into another script, will cause headaches, or a function re-write. All other super-global arrays are available in functions anyway ($_POST,$_GET,$_REQUEST), but due to the reasons stated above, shouldn't be used in functions. *hope I didn't mis-understand the question*. Quote Link to comment https://forums.phpfreaks.com/topic/246425-about-registering-globals/#findComment-1265458 Share on other sites More sharing options...
Hall of Famer Posted September 5, 2011 Author Share Posted September 5, 2011 I see, thanks for your reply and sorry for the confusion. The idea here is that it is quite tedious to always write Mysql codes to retrieve a certain set of data again and again, this was why $GLOBALS was registered in the first place. Again please allow me to show you an example: Without registering $GLOBALS, the following lines have to be written whenever the script needs to retrieve data from mysql table: $result = mysql_query( "SELECT * FROM {$prefix}users WHERE uid = '$uid'"); $row = mysql_fetch_array($result); $money = $row['money']; It may not look tedious for this time being, but it can be annoying if you have to write the same code over and over again and in different script files. With registering $GLOBALS, its possible to retrieve information simply by writing $GLOBALS['money']. This act makes retrieving and updating database info much easier, and it is why I am hesitant what to do with registered $GLOBALS. Quote Link to comment https://forums.phpfreaks.com/topic/246425-about-registering-globals/#findComment-1265527 Share on other sites More sharing options...
Pikachu2000 Posted September 5, 2011 Share Posted September 5, 2011 If you're trying to avoid writing the same code over and over, use a function. If you need to make a value available globally, why not use a $_SESSION var? Quote Link to comment https://forums.phpfreaks.com/topic/246425-about-registering-globals/#findComment-1265528 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.