EchoFool Posted March 10, 2009 Share Posted March 10, 2009 I am trying to create a variable in a function to pass it back out to the script that called it but it does not seem to like it... i get undefined variable but in the function the variable is set because I echo'd it.... this is generally what i did: <?php function callfunction(){ $Cheese = 1; Echo $Cheese.'<br>'; } callfunction(); Echo $Cheese; ?> The result is this: 1 Notice: Undefined variable: Cheese on line 7 Have I done something wrong here? Quote Link to comment https://forums.phpfreaks.com/topic/148840-solved-pass-variables-back-from-function/ Share on other sites More sharing options...
lonewolf217 Posted March 10, 2009 Share Posted March 10, 2009 couple things wrong here. 1) Cheese is a local variable to the function. your main php page has no knowledge of this variable 2) you aren't returning any value from your function <?php function callfunction() { $cheese = 1; return $cheese } $value = callfunction(); echo $value; // this will echo "1" ?> Quote Link to comment https://forums.phpfreaks.com/topic/148840-solved-pass-variables-back-from-function/#findComment-781567 Share on other sites More sharing options...
PFMaBiSmAd Posted March 10, 2009 Share Posted March 10, 2009 That's not how functions are designed to be used. Functions return values (remember your high school math classes.) <?php function callfunction(){ $Cheese = 1; return $Cheese; } echo callfunction(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/148840-solved-pass-variables-back-from-function/#findComment-781569 Share on other sites More sharing options...
EchoFool Posted March 10, 2009 Author Share Posted March 10, 2009 couple things wrong here. 1) Cheese is a local variable to the function. your main php page has no knowledge of this variable 2) you aren't returning any value from your function <?php function callfunction() { $cheese = 1; return $cheese } $value = callfunction(); echo $value; // this will echo "1" ?> What if you wanted to return multiple variables? Quote Link to comment https://forums.phpfreaks.com/topic/148840-solved-pass-variables-back-from-function/#findComment-781570 Share on other sites More sharing options...
lonewolf217 Posted March 10, 2009 Share Posted March 10, 2009 return them as an array Quote Link to comment https://forums.phpfreaks.com/topic/148840-solved-pass-variables-back-from-function/#findComment-781572 Share on other sites More sharing options...
Philip Posted March 10, 2009 Share Posted March 10, 2009 You'd use an array function someFunc( ) { $array = array('foo', 'bar'); return $array; } $myValue = someFunc(); // myValue now is an array with [0] => "foo" and [1] => "bar" Quote Link to comment https://forums.phpfreaks.com/topic/148840-solved-pass-variables-back-from-function/#findComment-781573 Share on other sites More sharing options...
EchoFool Posted March 10, 2009 Author Share Posted March 10, 2009 Ok thank you. Quote Link to comment https://forums.phpfreaks.com/topic/148840-solved-pass-variables-back-from-function/#findComment-781576 Share on other sites More sharing options...
EchoFool Posted March 10, 2009 Author Share Posted March 10, 2009 Ok just ran into a new problem sorry I get this error now: Warning: Missing argument 1 for travellingbuttons() Warning: Missing argument 2 for travellingbuttons() Warning: Missing argument 3 for travellingbuttons() Warning: Missing argument 4 for travellingbuttons() Warning: Missing argument 5 for travellingbuttons() Warning: Missing argument 6 for travellingbuttons() This is the script have added more to it: <?php function travellingbuttons($Field,$Table,$IDCode,$SizeX,$SizeY,$var){ $Get = mysql_query("SELECT X,Y FROM $Table WHERE UserID='{$_SESSION['Current_User']}'") Or die(mysql_error()); $row = mysql_fetch_assoc($Get); $YouX = $row['X']; $YouY = $row['Y']; $array = array($YouX, $YouY); return $array; } travellingbuttons('Y','coords','1',50,50,'1'); $GetArray = travellingbuttons(); $YouX = $GetArray[0]; $YouY = $GetArray[1]; ?> What is the error telling me exactly? Quote Link to comment https://forums.phpfreaks.com/topic/148840-solved-pass-variables-back-from-function/#findComment-781587 Share on other sites More sharing options...
Philip Posted March 10, 2009 Share Posted March 10, 2009 travellingbuttons('Y','coords','1',50,50,'1'); $GetArray = travellingbuttons(); should be $GetArray = travellingbuttons('Y','coords','1',50,50,'1'); Quote Link to comment https://forums.phpfreaks.com/topic/148840-solved-pass-variables-back-from-function/#findComment-781590 Share on other sites More sharing options...
EchoFool Posted March 10, 2009 Author Share Posted March 10, 2009 Oh ok so it counts the values in the brackets as the name also. Thanks for that Quote Link to comment https://forums.phpfreaks.com/topic/148840-solved-pass-variables-back-from-function/#findComment-781592 Share on other sites More sharing options...
grissom Posted March 10, 2009 Share Posted March 10, 2009 Just a quick thought, try substituting these two lines travellingbuttons('Y','coords','1',50,50,'1'); $GetArray = travellingbuttons(); with this one line : $GetArray = travellingbuttons('Y','coords','1',50,50,'1') One other thing - why are you passing so many arguments to the function - it doesn't appear to use all of them ?? Quote Link to comment https://forums.phpfreaks.com/topic/148840-solved-pass-variables-back-from-function/#findComment-781595 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.