|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2001-06-12 18:51 UTC] per dot persson at gnosjo dot pp dot se
The following script gave a parse error:
<? switch($var): ?>
<? case "alpha": ?>
<p>Alpha</p>
<? break; ?>
<!-- ... -->
<? endswitch; ?>
Parse error: parse error, expecting `T_CASE' or `T_DEFAULT' or `'}'' in <my-path>/bug.php on line 2
First I thought that this was really a bug, but then, while typing this report I realized that non-PHP-mode characters (like the two spaces) probably are converted to an "echo" instruction, so the PHP-parser sees something like the following code:
switch($var):
echo ' ';
case "alpha":
echo '<p>Alpha</p>';
break;
endswitch;
The parser doesn't expect anything (but PHP-mode white-space) to come between the ':' on line 1 and 'case' on line 3 and therefore reports parse error. Right?
For the sake of "pretty-scripting", I would like this to change, but at the same time, now that I understand why the parser complains, I'm not sure if it should be changed...
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 20:00:01 2025 UTC |
Use the {} instead of the : / endswitch; ie. <?php switch ($value) { case: ?> <p>Alpha</p> <?php break; } ?> And anyway, using <? ?> too much is really not good idea. This is just stupid: <? some code ?><? more code?><? code..?>Which style I use, the "classical" style (using {}) or the "alternative" style (using : and endswitch), doesn't matter; the problem is the same. Sniper's comment about using <? ?> several times is surely justified when having one-line code as in his example, but when it comes to multi-line code, there is an issue of readability. I think that script A below is easier to read than script B. Don't you? --- begin script A --- <? switch($var): ?> <? case "alpha": ?> <p>Alpha</p> <? break; ?> <? case "beta": ?> <p>Beta</p> <? break; ?> <? default: ?> <p>Default</p> <? break; ?> <? endswitch; ?> --- end script A --- --- begin script B --- <? switch($var): case "alpha": ?> <p>Alpha</p> <? break; case "beta": ?> <p>Beta</p> <? break; default: ?> <p>Default</p> <? break; endswitch; ?> --- end script B ---The bug is valid - if you close and reopen the PHP tags between the switch and the case statement, it is going to bark at you. It shouldn't. This does not work: <? switch($var){ ?> <? case "alpha": break; }?> This does: <? switch($var){ case "alpha": break; }?> Why in the world would anyone do that, is a whole other issue, still they probably expect to be able to do just that. If they can't, we either need to document it as such, (thus re-classify this as documentation problem) or fix it. I do not know what it takes to fix that, but it is a valid report nevertheless. Unbogusifying...Observe that the spaces are significant. This doesn't work: <? switch($var){ ?> <? case "alpha": break; }?> This does!: <? switch($var){ ?> <? case "alpha": break; }?> I suppose that the interpreter converts text outside <? ?> to echo statements, so that the first code block is equivalent to: <? switch($var){ echo ' '; case "alpha": break; }?> This code block gave the same parse error as the first one! (Expecting T_CASE or T_DEFAULT or '}')Spaces are indeed significant. Here's why. <? switch($var){ ?> <? case "alpha": break; }?> Is equivalent to <? switch($var) { print " "; break; }?> The two spaces outside the PHP blocks are a valid two character HTML block! Whereas: <? switch($var){ ?> <? case "alpha": break; }?> is equivalent to: <? switch($var){ case "alpha": break; }?> which is valid. Vlad was wrong, the bug is not valid :)Could you guys update the documentation to reflect this? Just reading the sections on switches and the alternate syntax, I thought that this would work.. <?php switch ( $fruit ): ?> <?php case 'pears': ?> I understand Zeev's response to Vlad, and having to do this instead... <?php switch ( $fruit ): php case 'pears': ?> but there's no mention of this in the docs.<? switch($var){ ?> <? case "alpha": break; }?> should be equivalent to <? switch($var){ print "\n" case "alpha": break; }?> so why does it work if this <? switch($var){ print " " case "alpha": break; }?> does not work ?