Bug #11447 Updated: Switch statement sensible for white-space
| From: | zeev@php.net | Date: | Sun, 15 Jul 2001 20:47:32 +0000 |
| Subject: | Bug #11447 Updated: Switch statement sensible for white-space | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-60063@lists.php.net to get a copy of this message | ||
ID: 11447
Updated by: zeev
Reported By: per.persson@gnosjo.pp.se
Old Status: Open
Status: Closed
Bug Type: Scripting Engine problem
Operating System: Linux (RedHat 6.2)
PHP Version: 4.0.4pl1
New Comment:
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 :)
Previous Comments:
------------------------------------------------------------------------
[2001-06-19 17:16:53] per.persson@gnosjo.pp.se
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 '}')
------------------------------------------------------------------------
[2001-06-19 12:29:47] vlad@php.net
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...
------------------------------------------------------------------------
[2001-06-13 14:58:29] per.persson@gnosjo.pp.se
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 ---
------------------------------------------------------------------------
[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?
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/?id=11447
Edit this bug report at http://bugs.php.net/?id=11447&edit=1