Jump to content


Photo

Variable passing to Function


  • Please log in to reply
5 replies to this topic

#1 famous58

famous58
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 22 May 2006 - 05:14 PM

I am stumped.

Why doesn't this code work?

<?
$cartUser = 'test';

ShowCart();

function ShowCart(){
  
  echo $cartUser;

}

?>

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).
I'm trying, really I am.

#2 famous58

famous58
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 22 May 2006 - 05:33 PM

Nevermind. Got this to work:

<?
$cartUser = 'test';

ShowCart($cartUser);

function ShowCart($cartUser){
  
  echo $cartUser;

}

?>

I'm trying, really I am.

#3 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 22 May 2006 - 07:59 PM

Time to [a href=\"http://us2.php.net/variables.scope\" target=\"_blank\"]read up [/a]on variable scope.

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 22 May 2006 - 07:59 PM

well just so you know, the way to use global is this:

<?
$cartUser = 'test';

ShowCart();

function ShowCart(){
  global $cartUser;  

  echo $cartUser;

}

?>

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:

<?
$cartUser = 'test';

ShowCart($cartUser);

function ShowCart($cartUser){
  
  echo $cartUser;

}

?>
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:

<?
$cartUser = 'test';

ShowCart($cartUser);

echo $cartUser; //2nd echo

function ShowCart($cartUser){
  
  $cartUser .= ' 123';
  echo $cartUser; //1st echo

}
?>

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:

test 123

then after the function call you go to echo $cartUser again, but this is what will be displayed:

test

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:

ShowCart(&$cartUser) { .... }




Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 famous58

famous58
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 22 May 2006 - 08:13 PM

[!--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) View Post[/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:

ShowCart(&$cartUser) { .... }
[/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:

<?
$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;

}

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?
I'm trying, really I am.

#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 22 May 2006 - 08:35 PM

you will need to specifically declare it as a global variable in each function.

<?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();

this will produce:

0
1
<nothing>
<nothing>


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:

$example = 1;

function blah($example) {
    $example++;
    return $example;
}

$newnum = blah($example);

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users