2011-05-01 18 views
0

我該如何返回深度嵌套在if else結構中的變量,以用於另一個函數,該函數用於改變第一個函數中返回變量的結果中的程序流? 這是程序的基本結構,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 
    } 
} 

回答

4
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); 
1

那麼要麼你乾脆直接從函數讀取返回的變量:

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();