Bug #78304 [NEW]: Switch statement and first case cannot be in separate code blocks
From: benjamin dot morel at gmail dot com
Operating system: Fedora
PHP version: 7.3.7
Package: Scripting Engine problem
Bug Type: Bug
Bug description:Switch statement and first case cannot be in separate code blocks
Description:
------------
When using PHP as a template engine, it can be useful to break
statements into separate code blocks. For example, this works fine with
foreach:
<?php foreach ($users as $user): ?>
<span><?= ... ?></span>
<?php endforeach; ?>
The same thing does not work with switch:
<?php switch ($type): ?>
<?php case 'a': ?>
<span>...</span>
<?php break; ?>
<?php case 'b': ?>
<span>...</span>
<?php break; ?>
<?php endswitch; ?>
Which fails with a parse error:
> Parse error: syntax error, unexpected ' ', expecting endswitch
(T_ENDSWITCH) or case (T_CASE) or default (T_DEFAULT)
For this to work, the first case must be in the same code block as the
switch. This works fine:
<?php switch ($type):
case 'a': ?>
<span>...</span>
<?php break; ?>
<?php case 'b': ?>
<span>...</span>
<?php break; ?>
<?php endswitch; ?>
I believe this is a bug.
Test script:
---------------
<?php $type = 'a'; ?>
<?php switch ($type): ?>
<?php case 'a': ?>
This is A
<?php break; ?>
<?php case 'b': ?>
This is B
<?php break; ?>
<?php endswitch; ?>
Expected result:
----------------
This is A
Actual result:
--------------
Parse error: syntax error, unexpected ' ', expecting endswitch
(T_ENDSWITCH) or case (T_CASE) or default (T_DEFAULT) in ...
--
Edit bug report at https://bugs.php.net/bug.php?id=78304&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=78304&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=78304&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=78304&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=78304&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=78304&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=78304&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=78304&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=78304&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=78304&r=support
Expected behavior: https://bugs.php.net/fix.php?id=78304&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=78304&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=78304&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=78304&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=78304&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=78304&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=78304&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=78304&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=78304&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=78304&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=78304&r=mysqlcfg
Thread (7 messages)
- benjamin dot morel at gmail dot com