2011-07-05 3 views
1

IF 내의 세 함수 중 하나라도 성공적으로 실행되지 않았는지 확인하고 싶습니다. 그들 중 하나가 실행되지 않은 경우, 나는 반환 값 false를 원합니다.PHP 함수 반환에 관한 질문

if($ext == "gif" or $ext == "png"){ 
    imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127)); 
    imagealphablending($new, false); 
    imagesavealpha($new, true); 
    } 

예를 들어 나는 모든 세 가지 기능의 imagecolortransparent이 imagealphablending, imagesavealpha이되지 않을 경우 false를 반환, 성공적으로 실행 않은 경우 알고 싶어요. 다음과 같은 각 기능을 확인해야합니까 아니면 더 좋은 방법이입니까?

if($ext == "gif" or $ext == "png"){ 
    if ([email protected]($new, imagecolorallocatealpha($new, 0, 0, 0, 127))) 
     return false; 
    if ([email protected]($new, false)) 
     return false; 
    if ([email protected]($new, true)) 
     return false; 
} 

감사합니다.

+0

__better__ 란 무엇입니까? – gnur

+0

내 이미지 리사이즈 함수에서 많은 이미지를 사용하여 관련된 함수를 사용하여 각 함수 반환 값을 개별적으로 반환해야하기 때문에 각 함수에 대해 false를 반환해야하는지 궁금 해서요. – sunjie

답변

0

하나의 단일 if로 조인 할 수 있지만 함수 호출 중 하나가 실패하면 다음 명령문을 실행하지 않기 때문에 아마도 더 나은 방법 일 수 있습니다.

+2

부 울린 '&&'연산자는 단락되어'false'가 발생할 때 실행을 중지합니다. – deceze

0

모든 PHP 오류를 ErrorException으로 변환하고이를 전역으로 등록하는 사용자 정의 오류 처리기를 설정할 수 있습니다. 이 작업을 완료하면 세 함수 중 하나에서 오류가 발생하여 try/catch 블록으로 처리 할 수있는 예외가 발생합니다.

0
if ([email protected]($new, imagecolorallocatealpha($new, 0, 0, 0, 127)) 
    || [email protected]($new, false) 
    || [email protected]($new, true)) return false; 

하거나

return (@imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127)) 
    && @imagealphablending($new, false) 
    && @imagesavealpha($new, true)); 

모르겠 @ 여기에 필요한 경우. 당신은 그것을 피하려고 노력해야합니다.

0

당신이하는 일은 꽤 괜찮은 것 같아요.하지만 표현식을 연결할 수는 있습니다. 이 같은 일을, 그리고 경우에 함수, 표현식이 나머지 (런타임 최적화) 동안 추적되지 않습니다 false를 반환 :

if($ext == "gif" or $ext == "png"){ 
    if (
     @imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127)) 
     and @imagealphablending($new, false) 
     and @imagesavealpha($new, true) 
    ) return true; 
    return false; 
} 

또는 을 두 번째를 살려주는 변수를 사용하여 으로하여보다 그것을 만드는 경우 표현형 :

if($ext == "gif" or $ext == "png") 
    return 
     @imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127)) 
     && @imagealphablending($new, false) 
     && @imagesavealpha($new, true) 
    ; 
0

다음은 어떻게 사용합니까?

if($ext == "gif" or $ext == "png"){ 
    $return = TRUE; 
    $return = $return && imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127)); 
    $return = $return && imagealphablending($new, false); 
    $return = $return && imagesavealpha($new, true); 
    return $return; 
} 

는 기본적으로 나는 And들이 모두 해당하는지 확인하기 위해 모든 응답을 -ing입니다. 이들 중 하나가 FALSE을 반환하면 함수는 FALSE을 반환합니다.

더 아름다운 방법이 있지만, 나는이 방법으로 정확히 무슨 일이 일어나고 있는지 명확하게 알 수 있습니다.

+0

내가 downvote 이유를 물어 봐도 될까요? 나는 항상 건설적인 비판에 열려 있습니다. – Icode4food

1
if($ext == "gif" or $ext == "png"){ 
if (([email protected]($new, imagecolorallocatealpha($new, 0, 0, 0, 127))) 
    || ([email protected]($new, false)) 
    || ([email protected]($new, true))) 
    { 
     return false; 
    } 

}  

이것은 또한 동일한 기능을 수행합니다. 그러나 두 가지 방법 모두에서 어떤 기능이 실패했는지 알지 못할 것입니다.