2012-07-08 2 views
2

사용자 등록 스크립트가 있습니다. 한 단계에서 나는 세 번이나 방법을 호출합니다. 메서드가 true를 반환하는지 확인하고 그렇지 않으면 오류 메시지가 포함 된 문자열을 반환하고 반환 된 문자열을 가져 와서 변수에 넣습니다.메서드를 여러 번 호출하면 더 생산적인 방법이 있습니까?

더 생산적인 방법으로이 방법을 한 번만 호출하면됩니다. 그래도 내가 필요한 모든 대답을 얻을 수 있습니까?

을 Heres 코드 : 당신은 if 문에 변수를 할당 할 수

//check thumbnail is present and good 
      if($register->checkThumb()){ 
       //send image to permanent image directory 
       $register->moveUploadedImage(); 

       //if the thumbnail failed validation put the error message in variable 
      }else if(is_string($register->checkThumb())){ 
       $message = $register->checkThumb(); 

      } 
+0

이유 투표가? – crm

답변

1
$thumb = $register->checkThumb(); //call method once and save in variable 
    /* using just if($thumb) would return always true, because 
     the function may returns an errormessage on failure 
     which is ja string, which is not empty, not 0, not false == true */ 
    if($thumb === true){ 
     //send image to permanent image directory 
     $register->moveUploadedImage(); 
    }else{ //so then it's enough to ask for error this way 
     $message = $thumb; 
    } 
1

,

if($checked = $register->checkThumb()){ 
    //send image to permanent image directory 
    $register->moveUploadedImage(); 

    //if the thumbnail failed validation put the error message in variable 
}else if(is_string($checked)){ 
    $message = $checked; 

} 
1
다음과 같이 할 수

:

if(!($check_thumb_retvalue = $register->checkThumb())) { 
    //send image to permanent image directory 
    $register->moveUploadedImage(); 

//if the thumbnail failed validation put the error message in variable 
} 
else if(is_string($check_thumb_retvalue)) { 
    $message = $register->checkThumb(); 
} 

또는, 더 읽기를 :

$check_thumb_retvalue = $register->checkThumb(); 
if(!$check_thumb_retvalue){ 
    //send image to permanent image directory 
    $register->moveUploadedImage(); 
} 
//if the thumbnail failed validation put the error message in variable 
else if(is_string($check_thumb_retvalue)) { 
    $message = $check_thumb_retvalue; 
} 

LG, CK

1

당신은 할 수 :

 $result = $register->checkThumb(); 
     if($result){ 
      //send image to permanent image directory 
      $register->moveUploadedImage(); 

      //if the thumbnail failed validation put the error message in variable 
     }else if(is_string($result)){ 
      $message = $result; 

     } 

하지만 방법이 전혀 눈에 띄는 차이가있을려고하고 있지 않다 매우 비싼하지 않는 한 코드가 괜찮습니다.

1

결과를 변수에 할당 한 다음 해당 변수를 확인할 수 있습니다. 또한 변수가 true인지 확인하는 경우 === 연산자를 사용해야합니다. 그렇지 않으면 함수가 비어 있지 않은 문자열을 반환하면 true로도 채워집니다. 연산자 === 유형을 검사합니다. 그러면 true 값의 부울 변수 만 통과합니다.

$result = $register->checkThumb(); 
if($result === true) { 
    $register->moveUploadedImage(); 
} else if (is_string($result)){ 
    $message = $result; 
} 
관련 문제