我在几个方面尝试了相同的编码,但没有一个能够工作。
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
任何想法为什么?
答案 0 :(得分: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);
}
}
答案 1 :(得分:2)
是的,语法错误。您不能在单个else
语句中包含多个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
声明,如果您期望更多选项。