syntax error, unexpected ‘elseif’

Description

The elseif must follow a if structure. It cannot be used alone, not be used after a else clause.

Example

<?php

// elseif cannot be standalone
elseif ($a === 1) {
    echo 2;
}

// else has closed the if structure, not more elseif allowed
if ($condition) {}
else {}
elseif ($a === 1) {
    echo 2;
}

// the endif; has already closed the if structure.
if ($condition):

endif;
elseif ($a === 1) :
    echo 2;
endif;

?>

Solutions

  • Ensure that there is a previous if structure, with one then block, and optionally, other elseif block.

  • Ensure that there is no previous else clause.

  • Ensure that there is no previous endif keyword.