我的php脚本中有4个函数,它将访问升序,在我想要返回值的最后一个函数中,但是赋值不起作用。
我的php脚本:
function fourth_f($var){
$var = $var + 1;
return $var; //this will make the $var = 6 and return to main process
}
function third_f($var){
$var = $var + 1;
fourth_f($var); //this will make the $var = 5
}
function second_f($var){
$var = $var + 1;
third_f($var); //this will make the $var = 4
}
function first_f($var_1, $var_2){
$var = $var_1 + $var_2 + 1;
second_f($var); //this will make the $var = 3
}
//The main
$var_1 = 1;
$var_2 = 1;
$final_var = first_f($var_1, $var_2);
//And i want to echo it here
echo $final_var;
当我执行脚本时,没有错误,但也没有结果,它只是空白,我期待结果为6。
任何人都知道什么是错的,我怎样才能正确地从最后一个函数中返回一个值?
答案 0 :(得分:2)
您的第四个函数会将值返回到firth函数。所以你必须在每个函数中返回每个函数调用,然后才能将值赋给变量,例如
function fourth_f($var){
$var = $var + 1;
return $var; //returns this value to function third_f
}
function third_f($var){
$var = $var + 1;
return fourth_f($var); //returns this value to function second_f
}
function second_f($var){
$var = $var + 1;
return third_f($var); //returns this value to function first_f
}
function first_f($var_1, $var_2){
$var = $var_1 + $var_2 + 1;
return second_f($var); //returns this value to the assignment
}