co.ador Posted March 9, 2010 Share Posted March 9, 2010 function get_global_variable_value($name) { return $GLOBALS[$name]; } $num = 10; $value = get_global_variable_value("num"); print $value; The function above uses the same function name for two different purposes and getting different values only by assigning a different argument that probably will have a different value than the original function argument above. the above script prints 10 how is that? Quote Link to comment Share on other sites More sharing options...
nafetski Posted March 9, 2010 Share Posted March 9, 2010 Ok, pardon me if this sounds wrong but... It looks like what you are doing is a horrible idea. I'm not 100% sure what you're trying to do, but it looks like a horrible idea. Are you trying to reference a variables value by it's name? Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted March 9, 2010 Share Posted March 9, 2010 function get_global_variable_value($name) { return $GLOBALS[$name]; } $num = 10; $value = get_global_variable_value("num"); print $value; the above script prints 10 how is that? $num has a value of 10 within global scope. $GLOBALS is an array of all global scope variables. $GLOBALS['num'] is the same as accessing $num as though it was in scope. As always, check the manual if you're confused about a part of the language: globals With all that said, don't use the 'global' keyword or the $GLOBALS array. It leads to poor code. Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted March 9, 2010 Share Posted March 9, 2010 function get_global_variable_value($name) { return $GLOBALS[$name]; } $num = 10; $value = get_global_variable_value("num"); print $value; the above script prints 10 how is that? $num has a value of 10 within global scope. $GLOBALS is an array of all global scope variables. $GLOBALS['num'] is the same as accessing $num as though it was in scope. As always, check the manual if you're confused about a part of the language: globals With all that said, don't use the 'global' keyword or the $GLOBALS array. It leads to poor code. Second that. I used to have a bad habit of declaring global variables when inside a function if I didn't want to have to pass those variables directly. It's just a bad idea. ..and writing a function to return a global variable (I would have thought) was pointless as you can get the global variable any way with $GLOBALS['num']. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 9, 2010 Share Posted March 9, 2010 Edit: Nightslyr beat me to it, but I'll post this anyway: function get_global_variable_value($name) { return $GLOBALS[$name]; } $num = 10; $value = get_global_variable_value("num"); print $value; The function above uses the same function name for two different purposes and getting different values only by assigning a different argument that probably will have a different value than the original function argument above. the above script prints 10 how is that? This is all about variable scope. The process above is pretty strait-forward. When you set $num = 10 it is done outside of any function so it has "global" scope. When you are then IN a function that variable is not directly available. FOr example, if you defined $num inside a function it is a different instance of $num. However, you can reference global variables in a couple of ways. 1. Initiate the gloabl variable within the function function name() { global $num; } In that case $num inside the function is the same variable as $num outside the function 2. Using $GLOBALS keyword as in the script above. It is an associate array of all the gloabl variables. So, since $num was defined outside the function it is gloabl and you can reference it inside a function, any function, using $GLOBALS['num'] See the manual here: http://php.net/manual/en/reserved.variables.globals.php Although I agree that function looks to be a bad idea, but is constructive for understanding the behavior Quote Link to comment Share on other sites More sharing options...
co.ador Posted March 10, 2010 Author Share Posted March 10, 2010 I think it's just a constructive to see how that will work. was not clear about. To be totally clear global scope means that any variable defined outside of a function can be accessed from any part of the script right, but not from inside a function. In order to bring a variable in the global scope inside of a function would you used the global, or $Globals variables to suck it in let say.... Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 10, 2010 Share Posted March 10, 2010 Pretty much. Look at the link I posted. The first example should give you a perfect idea of how it works. 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.