由于这在谷歌中排名如此之高,而且在找到对如此简单的东西的明确答案时,我会这样做。
1. 提供完整的答案,并彻底澄清如何将你实际上独立于你的职能之外的多种变数退回:
function myFunction() {
//your code that returns your results to variables below
$var1 = message 1 ;
$var2 = message 2 ;
$var3 = message 3 ;
// must put all variables inside an array to get them outside of function
$return_array = ($var1, $var2, $var3);
// return your array inside function
return $return_array;
}
// call function (returns all variables in array)
$get_results = myFunction();
// call and assign individual variables outside of function
$var1 = $get_results[0];
$var2 = $get_results[1];
$var3 = $get_results[2];
// now you can use individual variables outside of function for example:
echo $var2; //will now output: message 2
echo $var3; //will now output: message 3
如果你利用数据库查询结果,你将比可能更需要将链接变数输入这一功能。 例如,我们将说,db美元是我们的数据库连接,你只是修改以下两条法典:
//original code to change
function myFunction() {
//change to
function myFunction($db) {
//original code to change
$get_results = myFunction();
//change to
$get_results = myFunction($db);