famous58 Posted May 22, 2006 Share Posted May 22, 2006 I am stumped.Why doesn't this code work?[code]<?$cartUser = 'test';ShowCart();function ShowCart(){ echo $cartUser;}?>[/code]It seems like everytime I put variables outside of a function they do not pass through. Someone told me to use global but that doesn't work either (or perhaps I'm using it wrong). Quote Link to comment https://forums.phpfreaks.com/topic/10196-variable-passing-to-function/ Share on other sites More sharing options...
famous58 Posted May 22, 2006 Author Share Posted May 22, 2006 Nevermind. Got this to work:[code]<?$cartUser = 'test';ShowCart($cartUser);function ShowCart($cartUser){ echo $cartUser;}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10196-variable-passing-to-function/#findComment-38000 Share on other sites More sharing options...
448191 Posted May 22, 2006 Share Posted May 22, 2006 Time to [a href=\"http://us2.php.net/variables.scope\" target=\"_blank\"]read up [/a]on variable scope. Quote Link to comment https://forums.phpfreaks.com/topic/10196-variable-passing-to-function/#findComment-38030 Share on other sites More sharing options...
.josh Posted May 22, 2006 Share Posted May 22, 2006 well just so you know, the way to use global is this:[code]<?$cartUser = 'test';ShowCart();function ShowCart(){ global $cartUser; echo $cartUser;}?>[/code]that way you can use it locally inside the function. As far as passing it as an argument to the function like you did the 2nd time:[code]<?$cartUser = 'test';ShowCart($cartUser);function ShowCart($cartUser){ echo $cartUser;}?>[/code]this "works" in your case, because all you are doing is echoing the variable. But just so you know, when you manipulate the variable inside the function, for example, if you were to do this:[code]<?$cartUser = 'test';ShowCart($cartUser);echo $cartUser; //2nd echofunction ShowCart($cartUser){ $cartUser .= ' 123'; echo $cartUser; //1st echo}?>[/code]okay first you $cartUser gets set to 'test' then you call your function, which adds ' 123' to the end of it, and then echos $cartUser, which will display this:[b]test 123[/b]then after the function call you go to echo $cartUser again, but this is what will be displayed:[b]test[/b]Why? because when you pass a variable to a function, the function makes a copy of the variable, for local use inside the function. That's the difference between global and local scope. If you wanted that 2nd echo to print the same thing (that is, change the $cartUser on a global level), you would have to pass it by reference, by adding an ampersand before the variable in the function argument like this:[b]ShowCart(&$cartUser) { .... }[/b] Quote Link to comment https://forums.phpfreaks.com/topic/10196-variable-passing-to-function/#findComment-38031 Share on other sites More sharing options...
famous58 Posted May 22, 2006 Author Share Posted May 22, 2006 [!--quoteo(post=376120:date=May 22 2006, 12:59 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 22 2006, 12:59 PM) [snapback]376120[/snapback][/div][div class=\'quotemain\'][!--quotec--]Why? because when you pass a variable to a function, the function makes a copy of the variable, for local use inside the function. That's the difference between global and local scope. If you wanted that 2nd echo to print the same thing (that is, change the $cartUser on a global level), you would have to pass it by reference, by adding an ampersand before the variable in the function argument like this:[b]ShowCart(&$cartUser) { .... }[/b][/quote]Thanks for the help, again [img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /] One question though.What if I have say four functions that I want to use $cartUser in. Initially I thought that declaring $cartUser global outside a function would delcare it globaly for the script and any function calling $cartUser would be able to use it. Is there a way to do that? Or do I have to declare $cartUser global in each seperate function? The way I ended up doing it was like so:[code]<?$cartUser = $session->username;function1 ($cartUser){ mysql query needing $cartUser;}function2 ($cartUser){ mysql query needing $cartUser;}function3 ($cartUser){ mysql query needing $cartUser;}function4 ($cartUser){ mysql query needing $cartUser;}[/code]It works and I'm not modifying the variable in any of the functions. However, I want to be sure that doing this isn't going to cause problems down the road. Seems this way is beter than declaring the variable then declaring it global in each function. Yes? Quote Link to comment https://forums.phpfreaks.com/topic/10196-variable-passing-to-function/#findComment-38039 Share on other sites More sharing options...
.josh Posted May 22, 2006 Share Posted May 22, 2006 you will need to specifically declare it as a global variable in each function. [code]<?php $blah = 0;function example1() { global $blah; echo $blah . "<br>";}function example2() { global $blah; $blah++; echo $blah . "<br>";}function example3() { echo $blah . "<br>";}function example4() { $blah++; echo $blah . "<br>";}example1(); example2();example3();example4();[/code]this will produce:01[i]<nothing><nothing>[/i]nothing displays when you call example3 and example4 because $blah was not declared as a global variable. therefore the functions treat them as local variables, and since $blah was not initialized inside the function, it doesn't exist, and therefore nothing is echoed. It depends on what you are trying to accomplish, even in the long run. if you want just one variable that can be manipulated by any of the functions, and say you alter the variable in one function and you want to do something else with that variable in another function, but you want the variable to stay altered from the first, then you are going to have to declare it as a global variable inside the function, or else pass it by reference (putting the & in front of it, in the function argument declaration as shown in my previous post). Or, you could return the variable after you were done with it, like this:[code]$example = 1;function blah($example) { $example++; return $example;}$newnum = blah($example);[/code]what happens is you call the blah function, passing the var $example. the function increments it by one, and returns the result (2), setting $newnum to 2. doing it your way would be optimal if you have this one variable and the function needs it, but you do not want to permanently change the variable after the function has used it. For example, you have there the username and you pass it to your functions. well, if your function somehow changes it, it will only be changed inside that function. Other pieces of your script would still use that variable and it would still be the same, regardless of what the function did to it. Quote Link to comment https://forums.phpfreaks.com/topic/10196-variable-passing-to-function/#findComment-38051 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.