2011-05-01 3 views
0

첫 번째 함수에서 반환 된 변수의 결과에서 프로그램 흐름을 변경하는 다른 함수에 사용하기 위해 if else 구조 내부에 깊이 중첩 된 변수를 어떻게 반환합니까? 이것은 프로그램의 기본 구조입니다. if 및 else 문은 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 
    } 
} 

또는는 :

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