I am getting Parse error: syntax error, unexpected '', expecting endswitch (T_ENDSWITCH) or case (T_CASE) or default (T_DEFAULT) in ...
with the following code
<?php switch ($oEx->getCode()): ?>
<?php default: ?>
Stuff
<?php endswitch; ?>
Whereas this is OK
<?php switch ($oEx->getCode()):
endswitch; ?>
Why? A work around is to add a dummy-case as below before the closing tag but I can't see why this should be necessary.
<?php switch ($oEx->getCode()): case -9999: break; ?>
<?php default: ?>
Stuff
<?php endswitch; ?>
See the manual:
Warning Any output (including whitespace) between a switch statement and the first case will result in a syntax error. For example, this is invalid:
<?php switch ($foo): ?>
<?php case 1: ?>
...
<?php endswitch ?>
Whereas this is valid, as the trailing newline after the switch statement is considered part of the closing ?> and hence nothing is output between the switch and case:
<?php switch ($foo): ?>
<?php case 1: ?>
...
<?php endswitch ?>
The issue isn't the ending PHP tag, it's the whitespace. I think if you modified your original code as below, it should get rid of your syntax error:
<?php switch ($oEx->getCode()): ?>
<?php default: ?>
Stuff
<?php endswitch; ?>
Notice the only change made is removing the newline between your switch
statement and the default
case.
After you have a switch
there's nothing in between beside a default
or a case
<?php switch ($oEx->getCode()): ?>
//You can't have anything else here
<?php default: ?>
Stuff
<?php endswitch; ?>
So you have to place a default or a case just nest to switch without a new line or anything else :
<?php switch ($oEx->getCode()): ?>
<?php default: ?>
Stuff
<?php endswitch; ?>
<?php switch (1):
case 1:?>
<div>foo1</div>
<?php break;?>
<?php default:?>
<div>foo2</div>
<?php endswitch; ?>
I try this code ,and his work