dmfgkdg Posted January 23, 2013 Share Posted January 23, 2013 I took the following code from http://www.w3schools...p_variables.asp <?php $a = 5; $b = 10; function myTest() { global $a, $b; $b = $a + $b; } myTest(); echo $b; (this outputs 15) ?> The echo of $b comes after the closing of the function. The output is 15. If you add another echo inside the function like below: <?php $a = 5; $b = 10; function myTest() { global $a, $b; $b = $a + $b; } echo $b; (this one outputs 10) myTest(); echo $b; (this one outputs 15) ?> then the output for that echo is 10. I understand that the function is calling the global variables, but shouldnt the echo inside the function equal 15 and the one outside equal 10?, since it only refers to the global "$b" and is outside the function? Or does the global call inside the function completely change $b for the rest of the script? Thanks guys (and gals) Quote Link to comment https://forums.phpfreaks.com/topic/273566-local-vs-global-variable-example-from-w3schools/ Share on other sites More sharing options...
Psycho Posted January 23, 2013 Share Posted January 23, 2013 (edited) . . . shouldnt the echo inside the function equal 15 . . . There is no echo inside the function. I'm not sure which echo you are referring to but the output is exactly as I would expect. When the script runs it will follow this logic: Define $a and $b as 5 and 10, respectively Define the function myTest() Echo the value of $b (which is currently 10 as defined at the beginning of the script) Call the function myTest() which . . . - Makes the scope of $a and $b global (so they have the values as defined outside the function) - Redefines $b as the sum of $a and $b (5 + 10) which is 15 - Function ends, execution picks up from where the function was called Echo the value of $b (which is now 15 since it was modified globally within the function) Edited January 23, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/273566-local-vs-global-variable-example-from-w3schools/#findComment-1407834 Share on other sites More sharing options...
gizmola Posted January 24, 2013 Share Posted January 24, 2013 What global does in php is indicate that the variables inside the function should be references to global variables of the same name. If you change them inside the function, you change the global variable. Quote Link to comment https://forums.phpfreaks.com/topic/273566-local-vs-global-variable-example-from-w3schools/#findComment-1407863 Share on other sites More sharing options...
KevinM1 Posted January 24, 2013 Share Posted January 24, 2013 More to the point: 1. W3Schools sucks as a learning resource. It is not affiliated in any way with the W3C, and is notorious for giving out false information. If you want to learn PHP, get yourself a good book (I recommend starting with Larry Ullman's: http://www.amazon.com/PHP-MySQL-Dynamic-Web-Sites/dp/0321784073/ref=sr_1_2?s=books&ie=UTF8&qid=1359031826&sr=1-2&keywords=ullman) and look at the documentation on the official PHP website. 2. You should learn what 'global' is and how it works, but don't use it in your own code. There's a far better, cleaner, more readable way to send values to a function. 'global' is a surefire sign of doing it wrong, and if any of the resources you're currently reading/using to learn PHP feature it, that means they're a bad resource. Quote Link to comment https://forums.phpfreaks.com/topic/273566-local-vs-global-variable-example-from-w3schools/#findComment-1407921 Share on other sites More sharing options...
silkfire Posted January 24, 2013 Share Posted January 24, 2013 As per KevinM1's statement, try to avoid both the global keyword and the array $GLOBALS. Sending a variable to a function or having it as part of a construct in a class is a much more reliable solution. Quote Link to comment https://forums.phpfreaks.com/topic/273566-local-vs-global-variable-example-from-w3schools/#findComment-1407928 Share on other sites More sharing options...
dmfgkdg Posted January 24, 2013 Author Share Posted January 24, 2013 Thank you. I see where I made my mistake and feel dumb for asking now. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/273566-local-vs-global-variable-example-from-w3schools/#findComment-1407991 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.