jjacquay712 Posted January 30, 2009 Share Posted January 30, 2009 I am trying to make the variable $times accessible outside of the function. I am using the global keyword to declare the variable. Here is my code: <?php $string = $_GET['text']; $string = eregi_replace(" +", " ", $string); $sort = explode(" ", $string); global $times; //Look Right Here************************************** function sortme($array) { $num_times = (count($array)) - 1; $again = true; while ( $again ) { $again = false; for ( $i = 0; $i < $num_times; $i++ ) { if ( $array[$i] < $array[$i + 1] ) { $mem = $array[$i + 1]; $array[$i + 1] = $array[$i]; $array[$i] = $mem; $again = true; $times++; //Look Right Here************************************** } } } return $array; } $array = sortme($sort); foreach ($array as $dim => $val) { echo $dim . ' => ' . $val . '<br />'; } echo '<br /><br />Number Of Flips: ' . $times; //Look Right Here************************************** ?> Am I doing it wrong? Quote Link to comment https://forums.phpfreaks.com/topic/143058-solved-global-variables-in-functions/ Share on other sites More sharing options...
gevans Posted January 30, 2009 Share Posted January 30, 2009 You can set the variable outside of the function; <?php $times = NULL; $string = $_GET['text']; EDIT: then move the global statement inside the function Quote Link to comment https://forums.phpfreaks.com/topic/143058-solved-global-variables-in-functions/#findComment-750261 Share on other sites More sharing options...
jjacquay712 Posted January 30, 2009 Author Share Posted January 30, 2009 thanks ill try it out Quote Link to comment https://forums.phpfreaks.com/topic/143058-solved-global-variables-in-functions/#findComment-750266 Share on other sites More sharing options...
jjacquay712 Posted January 30, 2009 Author Share Posted January 30, 2009 thanks work perfectly Quote Link to comment https://forums.phpfreaks.com/topic/143058-solved-global-variables-in-functions/#findComment-750268 Share on other sites More sharing options...
.josh Posted January 30, 2009 Share Posted January 30, 2009 OR...instead of disregarding scope, you could like, pass it as an argument to the function, and then you'd be a coding superhero instead of a superzero. Quote Link to comment https://forums.phpfreaks.com/topic/143058-solved-global-variables-in-functions/#findComment-750270 Share on other sites More sharing options...
jjacquay712 Posted January 30, 2009 Author Share Posted January 30, 2009 what do you mean pass as an argument? Example? Quote Link to comment https://forums.phpfreaks.com/topic/143058-solved-global-variables-in-functions/#findComment-750273 Share on other sites More sharing options...
.josh Posted January 30, 2009 Share Posted January 30, 2009 You know, like the same thing you did with $array. Quote Link to comment https://forums.phpfreaks.com/topic/143058-solved-global-variables-in-functions/#findComment-750274 Share on other sites More sharing options...
jjacquay712 Posted January 30, 2009 Author Share Posted January 30, 2009 im not sure why you would need to pass that variable into into the function in the first place... but it works, so im happy. Quote Link to comment https://forums.phpfreaks.com/topic/143058-solved-global-variables-in-functions/#findComment-750276 Share on other sites More sharing options...
gevans Posted January 30, 2009 Share Posted January 30, 2009 CV, Is using global that way really that bad?? When I first learnt php I used a book called Sams Teach Yourself PHP, MySQL and Apache and that method of remembering a variable is one straight out of the book. Quote Link to comment https://forums.phpfreaks.com/topic/143058-solved-global-variables-in-functions/#findComment-750277 Share on other sites More sharing options...
.josh Posted January 30, 2009 Share Posted January 30, 2009 It's not a matter of necessity, but of good practice. Sure, you can code around it, but later on down the road, your code will be harder to follow and debug and expand upon, because you're just not sure what can be changed where. That's the point of staying within scope. If you pass the value of something to a function, the function creates its own local variable. What it does inside that function will not change the variable on the outside. That way, you don't have to worry about your variable being overwritten if you need that original value later on. If you want the value returned to the outside world, you can return something from a function. If you were to try and use global variables in the same situation, you would have to make a temporary copy of the variable, then mess around with it, then assign the original value back to the global variable. When you pass something through an argument, you don't have to do all that; php does that step for you. Like Vegas, you know that whatever happens in the function, stays in the function. Same thing for classes. Classes have even more layers of scope. You can have variables accessible to code outside of the class (public). You can also make them only accessible within the class that made them (private), and accessible within its own class and any class that extends that class (protected). The whole point in functions, and moreso, object oriented programming, is that you have a piece of code that is self-contained so you don't ever have to touch the insides of it, ever again. In order to adhere to that philosophy, you must contain the scope of the variables. You don't want people being able to mess around with internal variables, from the outside. Doing so makes the function unstable. The only way you can have a stable, secure function or method or class is if you know for absolute certain that no matter what, when you call it, it's going to do the same thing, every single time. Allowing code to disregard scope breaks that security. I'm sorry you read in some book that it's okay to do that, but it's not. It's not good programming practice. Quote Link to comment https://forums.phpfreaks.com/topic/143058-solved-global-variables-in-functions/#findComment-750300 Share on other sites More sharing options...
gevans Posted January 30, 2009 Share Posted January 30, 2009 Ok, that all makes perfect sense. But for example, in this case you have a function that needs to return two sets of data (or should if we keep in scope). One being an array and the other a varialbe. What's the best way to do this within the confinds of a function...? Quote Link to comment https://forums.phpfreaks.com/topic/143058-solved-global-variables-in-functions/#findComment-750303 Share on other sites More sharing options...
.josh Posted January 30, 2009 Share Posted January 30, 2009 In this case, you can return a multi-dim array which would be the easiest, or you could array_push $times onto the end of $array (or array_unshift it to the beginning of $array, either way), and then array_pop or array_shift it back off, once it has been returned. Quote Link to comment https://forums.phpfreaks.com/topic/143058-solved-global-variables-in-functions/#findComment-750307 Share on other sites More sharing options...
gevans Posted January 30, 2009 Share Posted January 30, 2009 Fair enough, I thought there may be something 'prettier' that I was missing. I've used both the array, and global method. Quote Link to comment https://forums.phpfreaks.com/topic/143058-solved-global-variables-in-functions/#findComment-750309 Share on other sites More sharing options...
PFMaBiSmAd Posted January 30, 2009 Share Posted January 30, 2009 In you have to return more than what can be done with a variable or an array, you should either be using a class or the functionality of the code in the function is not well defined and should probably be part of the main code and not a function. Functions should be building blocks that perform useful and discrete operations that your application needs. Quote Link to comment https://forums.phpfreaks.com/topic/143058-solved-global-variables-in-functions/#findComment-750313 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.