2012-03-04 33 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

はい、構文が間違っています。複数のelse句を1つのifステートメントに含めることはできません。

代わり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

ブロックが2つあります。それは意味をなさないので許可されていません。

returnが実行可能であるため、両方の内容をマージしてください(最初は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); 
    } 
}