我如何返回一个深嵌套在if else结构中的变量,以便在另一个函数中使用,该函数从第一个函数中返回的变量的结果中改变程序流?这是程序的基本结构,if和else语句可以包含更多的if else语句,这就是为什么我用了这个词。我该如何在第二个函数中使用变量呢?
。
function this_controls_function_2($flow) {
if($flow == 1) {
$dothis = 1;
return $dothis;
}
else {
$dothis = 2;
return $dothis;
}
}
function this_is_function_2() {
if($dothis == 1) {
//DO SOMETHING
}
else {
//DO SOMETHING
}
}
function this_is_function_2($flow) {
$dothis = this_controls_function_2($flow);
if($dothis == 1) {
//DO SOMETHING
}
else {
//DO SOMETHING
}
}
或者,如果您想调用函数2之外的第一个函数:
function this_is_function_2($dothis) {
if($dothis == 1) {
//DO SOMETHING
}
else {
//DO SOMETHING
}
}
$dothis = this_controls_function_2($flow);
this_is_function_2($dothis);
您可以直接从函数中读取返回的变量:
function this_is_function_2() {
if(this_controls_function_2($flow) == 1) {
//DO SOMETHING
}
else {
//DO SOMETHING
}
}
或者将变量标记为全局:
function this_controls_function_2($flow) {
global $dothis;
if($flow == 1) {
$dothis = 1;
return $dothis;
}
else {
$dothis = 2;
return $dothis;
}
}
function this_is_function_2() {
global $dothis;
if($dothis == 1) {
//DO SOMETHING
}
else {
//DO SOMETHING
}
}
为此,函数调用的顺序必须符合:
this_controls_function_2($flow);
/* ... */
this_is_function_2();