Bug #11447 Updated: Switch statement sensible for white-space
| From: | per dot persson at gnosjo dot pp dot se | Date: | Wed, 13 Jun 2001 18:58:30 +0000 |
| Subject: | Bug #11447 Updated: Switch statement sensible for white-space | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-57084@lists.php.net to get a copy of this message | ||
ID: 11447
User Update by: per.persson@gnosjo.pp.se
Status: Bogus
Bug Type: Scripting Engine problem
Operating system: Linux (RedHat 6.2)
PHP Version: 4.0.4pl1
Description: Switch statement sensible for white-space
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 ---
Previous Comments:
---------------------------------------------------------------------------
[2001-06-13 10:18:56] sniper@php.net
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..?>
---------------------------------------------------------------------------
[2001-06-12 19:13:44] per.persson@gnosjo.pp.se
Section 7.4 in the FAQ is a little bit related.
Should perhaps the ending for a block of PHP-code be "?>" followed by any number of
white-space characters?
---------------------------------------------------------------------------
[2001-06-12 18:51:27] per.persson@gnosjo.pp.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...
---------------------------------------------------------------------------
Full Bug description available at: http://bugs.php.net/?id=11447