newbtophp Posted May 6, 2010 Share Posted May 6, 2010 I've read in several tutorials, that globals within functions are considered 'dirty' im using them within my code, and was wondering if it could be improved in a more effective way? My code: <?php function rank($user_id){ global $rank, $level; $ptssql = mysql_query("SELECT * FROM site_users WHERE user_id = '$user_id'"); $ptsrow = mysql_fetch_array($ptssql); $points = $ptsrow['points']; if ($points <= 99){ $rank = "Jr Member"; $level = 0; } elseif ($points >= 100 && $points <= 299){ $rank = "Member"; $level = 1; } } //usage rank(47); ?> Your Rank: <?php echo $rank; ?> All help appreciated Quote Link to comment https://forums.phpfreaks.com/topic/200928-function-globals-are-dirty/ Share on other sites More sharing options...
Daniel0 Posted May 6, 2010 Share Posted May 6, 2010 Just pass the values as parameters instead. Quote Link to comment https://forums.phpfreaks.com/topic/200928-function-globals-are-dirty/#findComment-1054259 Share on other sites More sharing options...
newbtophp Posted May 6, 2010 Author Share Posted May 6, 2010 Just pass the values as parameters instead. The function selects a rank following a query which is dependant on the user_id parem, so im not sure how that'd work? Quote Link to comment https://forums.phpfreaks.com/topic/200928-function-globals-are-dirty/#findComment-1054262 Share on other sites More sharing options...
.josh Posted May 6, 2010 Share Posted May 6, 2010 point of a function is for a bit of code to be encapsulated with its own scope. Without that, you cannot guarantee that your function will work and/or return what it is supposed to 100% of the time. The better thing to do would be to pass those variables as arguments and return when you are done. Also your function should also validate those vars, make sure they are what they are supposed to be. Or pass validation off to a separate function...for example, you have that $user_id directly in a query... where is that coming from? Is it being validated somewhere else? Can the user somehow change/influence its value at some point in time before that query? Quote Link to comment https://forums.phpfreaks.com/topic/200928-function-globals-are-dirty/#findComment-1054264 Share on other sites More sharing options...
Daniel0 Posted May 6, 2010 Share Posted May 6, 2010 Just pass the values as parameters instead. The function selects a rank following a query which is dependant on the user_id parem, so im not sure how that'd work? Well, return the values in an array. Quote Link to comment https://forums.phpfreaks.com/topic/200928-function-globals-are-dirty/#findComment-1054265 Share on other sites More sharing options...
newbtophp Posted May 6, 2010 Author Share Posted May 6, 2010 @CV Im validating them before they are called to the function using (intval() or the int typecast along with a mysql_num_rows() to check it exists), and its only me who has the permissions to access the functions etc. - so its all prety safe. and @Daniel Cheers for the workaround. Quote Link to comment https://forums.phpfreaks.com/topic/200928-function-globals-are-dirty/#findComment-1054277 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.