cloudll Posted May 31, 2015 Share Posted May 31, 2015 Hey guys. I have been reading into global variables and its confusing me. I am trying to understand the best way to go about using variables within a function. I have been using the following format $var1 = "blah"; $var2 = "blah"; function phpfreaks() { global $var1, $var2; echo $var1; echo $var2; } The internet seems to hate variables used this way with a passion. Surely its okay for something like the PDO $dbh to be used as a global variable as this will never be changed? I'm still a big novice at all of this, would someone be able to edit my example please to show me the best approach to using external variables in my functions? Thanks for any help Quote Link to comment https://forums.phpfreaks.com/topic/296554-functions-and-global-variables/ Share on other sites More sharing options...
Solution jcbones Posted May 31, 2015 Solution Share Posted May 31, 2015 The reason global variables are bad, is because they can change. What happens if you start coding a new script and call the PDO variable $pdo instead of $dbh? However there are much better ways of doing things. $var1 = 'blah'; $var2 = 'blah'; function phpfreaks($v1,$v2) { return $v1 . ' ' . $v2; } echo phpfreaks($var1,$var2); If you want to make sure the correct type of variable is passed to the function, you can use type hinting. $var1 = 'blah'; $var2 = 'blah'; function phpfreaks(PDO $v1, $v2) { return $v1 . ' ' . $v2; } echo phpfreaks($var1,$var2); //return: error, as $v1 is not an object of PDO You can also pass by reference, so that the function has control of the variable outside of the function scope. $var1 = 'blah'; $var2 = 'blah'; function phpfreaks(&$v1, &$v2) { $v1 = 'Not Blah'; $v2 = 'Not Blah'; return; } if(phpfreaks($var1,$var2)) { echo $var1 . ' ' . $var2; } //output: Not Blah Not Blah Quote Link to comment https://forums.phpfreaks.com/topic/296554-functions-and-global-variables/#findComment-1512897 Share on other sites More sharing options...
Barand Posted May 31, 2015 Share Posted May 31, 2015 Pass them as arguments and have what you read about globals clinically erased. function phpfreaks() { global $pdo; ... return; } is fine until the day comes when you find yourself having to work with two simultaneous PDO connections, and all your functions are wired in to the first. Now you have to start swapping values. If you had used function phpfreaks($pdo) { ... return; } there would be no problems. Quote Link to comment https://forums.phpfreaks.com/topic/296554-functions-and-global-variables/#findComment-1512898 Share on other sites More sharing options...
cloudll Posted May 31, 2015 Author Share Posted May 31, 2015 Thanks for the replies guys. So I've taken that last example given, and I've passed the variable to the function. $data = 'My data'; function test($data) { echo $data; } But when I call the function its empty Quote Link to comment https://forums.phpfreaks.com/topic/296554-functions-and-global-variables/#findComment-1512905 Share on other sites More sharing options...
maxxd Posted May 31, 2015 Share Posted May 31, 2015 You've still got to pass the variable to the function. $data = 'My data'; function test($data) { echo $data; } test($data); Quote Link to comment https://forums.phpfreaks.com/topic/296554-functions-and-global-variables/#findComment-1512908 Share on other sites More sharing options...
cloudll Posted May 31, 2015 Author Share Posted May 31, 2015 Got ya, Thanks. I was still just using the function as test(); Where does return; fit into all this if it works without it ? Quote Link to comment https://forums.phpfreaks.com/topic/296554-functions-and-global-variables/#findComment-1512915 Share on other sites More sharing options...
Barand Posted May 31, 2015 Share Posted May 31, 2015 Functions normally return a value based on the arguments passed to it rather than echoing the output. Here's a trivial example function add ($a, $b) { return $a + $b; } echo add(2,3); Quote Link to comment https://forums.phpfreaks.com/topic/296554-functions-and-global-variables/#findComment-1512926 Share on other sites More sharing options...
cloudll Posted May 31, 2015 Author Share Posted May 31, 2015 Thanks for all the tips guys, learnt a lot. Quote Link to comment https://forums.phpfreaks.com/topic/296554-functions-and-global-variables/#findComment-1512937 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.