其他意想不到的? [关闭]

I tried this same coding in several ways, and none of them work.

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);
    }
}

The error happens in the commented line. It says Unexpected T_ELSE

Any ideas why?

Yes, the syntax is wrong. You can't have multiple else clauses in a single if statement.

You can use elseif instead:

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);
}

or a switch statement, if you're expecting more options.

You have two else blocks. That makes no sense and is thus not allowed.

You need to remove one of them, merge the contents of both (doesn't make sense though since only one return can be executed anyway) or turn the first one into an elseif(some condition) block.

With an elseif it would look like this; you just need to insert a condition to make it work:

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);
    }
}