devWhiz Posted July 28, 2011 Share Posted July 28, 2011 how would I do so? I did some googling and found this.. <?php function addandsubtract ($firstvalue, $secondvalue){ $firstreturnvalue = ($firstvalue + $secondvalue); $secondreturnvalue = ($firstvalue - $secondvalue); $myarray = array (); $myarray[0] = $firstreturnvalue; $myarray[1] = $secondreturnvalue; return $myarray; } $myarray = array (); $myarray = addandsubtract (10, 3); echo $myarray[0] . "<br />"; echo $myarray[1]; ?> is there a way possible to do t his using less code or...? Quote Link to comment https://forums.phpfreaks.com/topic/243135-return-more-than-one-variable-in-a-function/ Share on other sites More sharing options...
requinix Posted July 29, 2011 Share Posted July 29, 2011 It's just an array. Nothing more. return array($firstvalue + $secondvalue, $firstvalue - $secondvalue); Quote Link to comment https://forums.phpfreaks.com/topic/243135-return-more-than-one-variable-in-a-function/#findComment-1248803 Share on other sites More sharing options...
devWhiz Posted July 29, 2011 Author Share Posted July 29, 2011 now how would I call the variable? Quote Link to comment https://forums.phpfreaks.com/topic/243135-return-more-than-one-variable-in-a-function/#findComment-1248818 Share on other sites More sharing options...
Pikachu2000 Posted July 29, 2011 Share Posted July 29, 2011 The same way as before. Quote Link to comment https://forums.phpfreaks.com/topic/243135-return-more-than-one-variable-in-a-function/#findComment-1248821 Share on other sites More sharing options...
devWhiz Posted July 29, 2011 Author Share Posted July 29, 2011 calling variable outside of the function? Sorry I'm a little confused Quote Link to comment https://forums.phpfreaks.com/topic/243135-return-more-than-one-variable-in-a-function/#findComment-1248841 Share on other sites More sharing options...
Pikachu2000 Posted July 29, 2011 Share Posted July 29, 2011 Okay, explain what you meant by calling the variable. Quote Link to comment https://forums.phpfreaks.com/topic/243135-return-more-than-one-variable-in-a-function/#findComment-1248842 Share on other sites More sharing options...
devWhiz Posted July 29, 2011 Author Share Posted July 29, 2011 echoing each entry of the array in the function, outside of the function <?php $one = 20; $two = 25; $three = 30; function variable($a, $b, $c){ $a = $b + 10; $b = $c + 15; return $b; } $variable = variable($one, $two, $three); echo $variable; ?> the above echos the value for $b <?php $one = 20; $two = 25; $three = 30; function variable($a, $b, $c){ $a = $b + 10; $b = $c + 15; return array($a, $b); } $variable = variable($one, $two, $three); echo $variable; ?> that echos Array print_r shows both the $a and $b values, how would I echo just the $b value or just the $a value? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/243135-return-more-than-one-variable-in-a-function/#findComment-1248844 Share on other sites More sharing options...
Pikachu2000 Posted July 29, 2011 Share Posted July 29, 2011 That's what I thought you meant. You just echo them like you would any other array element. echo $variable[0]; echo $variable[1]; Quote Link to comment https://forums.phpfreaks.com/topic/243135-return-more-than-one-variable-in-a-function/#findComment-1248846 Share on other sites More sharing options...
devWhiz Posted July 29, 2011 Author Share Posted July 29, 2011 Ahhh I see I see, I don't know why that didnt register to me now is it possible to echo it without doing $variable = variable($one, $two, $three); just curious as to if its possible, thank you for the help btw, youre great Quote Link to comment https://forums.phpfreaks.com/topic/243135-return-more-than-one-variable-in-a-function/#findComment-1248849 Share on other sites More sharing options...
Ninjakreborn Posted July 29, 2011 Share Posted July 29, 2011 Ahhh I see I see, I don't know why that didnt register to me now is it possible to echo it without doing $variable = variable($one, $two, $three); just curious as to if its possible, thank you for the help btw, youre great I do not understand what you mean. Can you rephrase that question? Quote Link to comment https://forums.phpfreaks.com/topic/243135-return-more-than-one-variable-in-a-function/#findComment-1248866 Share on other sites More sharing options...
devWhiz Posted July 29, 2011 Author Share Posted July 29, 2011 I mean do I have to use $variable = variable($one, $two, $three); to access the array values the are being returned in the function? because to access what is being returned in the function I would have to do $variable[0], $variable[1], etc Quote Link to comment https://forums.phpfreaks.com/topic/243135-return-more-than-one-variable-in-a-function/#findComment-1248884 Share on other sites More sharing options...
silkfire Posted July 29, 2011 Share Posted July 29, 2011 If you don't want to deal with arrays another way to return more than one variable with a function is to use by reference with the ampersand symbol in front of the arguments of the function: function return_multiple(&$variable1, &$variable2, &$variable3) { $variable1 = 'something'; ..... } Call like this: return_multiple($var1, $var2, $var3) That will copy values into the variables you entered as arguments. Quote Link to comment https://forums.phpfreaks.com/topic/243135-return-more-than-one-variable-in-a-function/#findComment-1248888 Share on other sites More sharing options...
devWhiz Posted July 29, 2011 Author Share Posted July 29, 2011 & makes the variables global right? Quote Link to comment https://forums.phpfreaks.com/topic/243135-return-more-than-one-variable-in-a-function/#findComment-1248889 Share on other sites More sharing options...
silkfire Posted July 29, 2011 Share Posted July 29, 2011 Nope. & indicates that instead of working on a copy of the variable, work on the variable directly, allowing us to manipulate its value. I think you're making the problem look more complicated than it actually is: function addandsubtract($firstvalue, $secondvalue) { return array($firstvalue + $secondvalue, $firstvalue - $secondvalue); } $myarray = addandsubtract(10, 3); echo "$myarray[0]<br />$myarray[1]"; Quote Link to comment https://forums.phpfreaks.com/topic/243135-return-more-than-one-variable-in-a-function/#findComment-1248896 Share on other sites More sharing options...
devWhiz Posted July 29, 2011 Author Share Posted July 29, 2011 I did not write this <?php function addandsubtract ($firstvalue, $secondvalue){ $firstreturnvalue = ($firstvalue + $secondvalue); $secondreturnvalue = ($firstvalue - $secondvalue); $myarray = array (); $myarray[0] = $firstreturnvalue; $myarray[1] = $secondreturnvalue; return $myarray; } $myarray = array (); $myarray = addandsubtract (10, 3); echo $myarray[0] . "<br />"; echo $myarray[1]; ?> It was just one of the very few results I found when I was googling how to return in a function,trying to a find a solution to my problem Quote Link to comment https://forums.phpfreaks.com/topic/243135-return-more-than-one-variable-in-a-function/#findComment-1248903 Share on other sites More sharing options...
silkfire Posted July 29, 2011 Share Posted July 29, 2011 Didn't I solve your problem? What's your example code? Quote Link to comment https://forums.phpfreaks.com/topic/243135-return-more-than-one-variable-in-a-function/#findComment-1248906 Share on other sites More sharing options...
Pikachu2000 Posted July 29, 2011 Share Posted July 29, 2011 I think you have a basic misconception of what the arguments passed to function actually do. Due to variable scope, if the arguments aren't explicitly passed to the function, they are not available to the function. And no, I'm not going to advocate the use of 'global' to circumvent that. This will do nothing, as the values of the variables are not available within the function. $var1 = 10; $var2 = 20; $var3 = 30; function AddEmUp() { $total = ($var1 + $var2 + $var3); return $total; } echo AddEmUp(); Likewise this will do nothing (except throw a warning), because even though the arguments are defined, they are not passed to the function when it is called. $var1 = 10; $var2 = 20; $var3 = 30; function AddEmUp($var1, $var2, $var3) { // arguments are defined, but do not take the values of variables above $total = ($var1 + $var2 + $var3); // same as above. These vars are only relevant within the scope of the function return $total; } echo AddEmUp(); // no values passed to the function as arguments Only when the arguments are passed to the function in the function call does the function have the values available to it. $var1 = 10; $var2 = 20; $var3 = 30; function AddEmUp($var1, $var2, $var3) { // arguments are defined, but do not take the values of variables above because of scope $total = ($var1 + $var2 + $var3); // same as above. These vars are only relevant within the scope of the function return $total; } echo AddEmUp($var1, $var2, $var3); // previously assigned values are now passed to the function as arguments // will echo '60' /**** OR ****/ $result = AddEmUp($var1, $var2, $var3); // $result will now have a value of 60. A clearer, less confusing way to write the function that clearly shows the separation would be like this: $var1 = 10; $var2 = 20; $var3 = 30; function AddEmUp($arg1, $arg2, $arg3) { $total = ($arg1 + $arg2 + $arg3); return $total; } echo AddEmUp($var1, $var2, $var3); Hopefully that clears things up a little bit. Quote Link to comment https://forums.phpfreaks.com/topic/243135-return-more-than-one-variable-in-a-function/#findComment-1249048 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.