chris_2001 Posted September 20, 2008 Share Posted September 20, 2008 I want to create a function that will return more than 1 variable. What I want it to do is connect to a sql database by parameters you set with the function and return more than two pieces of data (in some cases 10). function test($table, $search, $value) { $query = "SELECT test1, test2, test3 FROM $table WHERE $search = '$value'"; $result = mysql_query($query); $row = mysql_fetch_array($result, MYSQL_ASSOC); $test1 = $row['test1']; $test2 = $row['test2']; $test3 = $row['test3']; } Above is an example of what I want it to do. How do I make it return $test1 - $test3? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/125114-solved-creating-a-function-to-return-more-than-1-variable/ Share on other sites More sharing options...
DarkWater Posted September 20, 2008 Share Posted September 20, 2008 You can only make it return one variable. It can return an array though, if you want. Quote Link to comment https://forums.phpfreaks.com/topic/125114-solved-creating-a-function-to-return-more-than-1-variable/#findComment-646611 Share on other sites More sharing options...
chris_2001 Posted September 20, 2008 Author Share Posted September 20, 2008 Yeah I was attempting to avoid using an array :/ if i were to return $test1 for example, once out of the function what would it read $test1 as? Could i do for example echo "$test1"; or would it be given a different name? Quote Link to comment https://forums.phpfreaks.com/topic/125114-solved-creating-a-function-to-return-more-than-1-variable/#findComment-646613 Share on other sites More sharing options...
DarkWater Posted September 20, 2008 Share Posted September 20, 2008 Functions have their own "namespace", so no. Quote Link to comment https://forums.phpfreaks.com/topic/125114-solved-creating-a-function-to-return-more-than-1-variable/#findComment-646619 Share on other sites More sharing options...
wildteen88 Posted September 20, 2008 Share Posted September 20, 2008 They only way is to return an array. Example function test($table, $search, $value) { $query = "SELECT test1, test2, test3 FROM $table WHERE $search = '$value'"; $result = mysql_query($query); $row = mysql_fetch_array($result, MYSQL_ASSOC); return $row; } $row = test($table, $search, $value); echo $row['test1']; echo $row['test2']; echo $row['test3']; // or alternatively, but not recommended extract($row); echo $test1; echo $test2; echo $test3; Quote Link to comment https://forums.phpfreaks.com/topic/125114-solved-creating-a-function-to-return-more-than-1-variable/#findComment-646625 Share on other sites More sharing options...
.josh Posted September 20, 2008 Share Posted September 20, 2008 if i were to return $test1 for example, once out of the function what would it read $test1 as? Could i do for example echo "$test1"; or would it be given a different name? if you return something from a function it will be assigned to the var that called the function, so... function something() { $x = rand(1,5); return $x; } // the random number would be assigned to $y... it would be "called" $y $y = something(); Quote Link to comment https://forums.phpfreaks.com/topic/125114-solved-creating-a-function-to-return-more-than-1-variable/#findComment-646628 Share on other sites More sharing options...
chris_2001 Posted September 20, 2008 Author Share Posted September 20, 2008 thx for the help Quote Link to comment https://forums.phpfreaks.com/topic/125114-solved-creating-a-function-to-return-more-than-1-variable/#findComment-646669 Share on other sites More sharing options...
discomatt Posted September 20, 2008 Share Posted September 20, 2008 I'm surprised no one mentioned pass by reference. <?php $a = 'hello'; $b = 'foo'; passByRef( $a, $b ); echo "$a<br />$b"; # A an argument is 'pass by reference' if an & is put before the variable function passByRef ( & $str1, & $str2 ) { $str1 .= ' world'; $str2 .= 'bar'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/125114-solved-creating-a-function-to-return-more-than-1-variable/#findComment-646725 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.