2012-03-04 2 views
0

여러 가지 방법으로이 동일한 코딩을 시도했지만 그 중 아무 것도 작동하지 않았습니다.예기치 않은 경우?

public function getCalendarById($calendarId) 
{ 
    $calendarsList = $this->getCalendarsList(); 
    if($calendarId == "1") { 
     return $this->getMergedCalendars(); 
    } else { 
     return (array_key_exists($calendarId, $calendarsList) ? $calendarsList[$calendarId] : null); 
    } else {//******** error here *********** 
     return (array_key_exists($calendarId, $calendarsList) ? $this->holidayrize($calendarId) : null); 
    } 
} 

주석 처리 된 줄에 오류가 발생합니다. 그것은 예기치 않은 T_ELSE를 말합니다

어떤 아이디어?

+2

첫 번째 else는 else if로 지정됩니다. –

+0

if (조건) ... else if (다른 조건) ... else – rosco

+0

'elseif'와 조건이 필요합니다. –

답변

2

예, 구문이 잘못되었습니다. 하나의 if 문에 여러 개의 else 절을 사용할 수 없습니다.

대신 elseif를 사용할 수 있습니다

if($calendarId == "1") { 
    return $this->getMergedCalendars(); 
} elseif (/* second condition here */) { 
    return (array_key_exists($calendarId, $calendarsList) ? $calendarsList[$calendarId] : null); 
} else { 
    return (array_key_exists($calendarId, $calendarsList) ? $this->holidayrize($calendarId) : null); 
} 

또는 switch 문, 당신은 더 많은 옵션을 기대하는 경우.

6

두 개의 다른 블록이 있습니다. 그것은 의미가 없으므로 허용되지 않습니다.

둘 중 하나를 제거하고 양쪽 내용을 병합해야합니다 (어쨌든 return을 실행할 수 있기 때문에 이해가되지 않습니다). 또는 첫 번째 것을 elseif(some condition) 블록으로 바꾸십시오.

elseif과 같이 보일 것입니다.

public function getCalendarById($calendarId) 
{ 
    $calendarsList = $this->getCalendarsList(); 
    if($calendarId == "1") { 
     return $this->getMergedCalendars(); 
    } 
    elseif(/*put some condition here*/) { 
     return (array_key_exists($calendarId, $calendarsList) ? $calendarsList[$calendarId] : null); 
    } 
    else { 
     return (array_key_exists($calendarId, $calendarsList) ? $this->holidayrize($calendarId) : null); 
    } 
}