2011-05-01 5 views
0

最初の関数で返された変数の結果からプログラムフローを変更する別の関数で使用するために、if else構造の内部に深くネストされた変数を返す方法はありますか? これはプログラムの基本的な構造ですが、if文とelse文にはelse文を含めることができます。 2番目の関数で変数をどのように使用しますか?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(); 
関連する問題