Jump to content

Recommended Posts

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. 

Link to comment
https://forums.phpfreaks.com/topic/246425-about-registering-globals/
Share on other sites

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*.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.