| Bug #25667 | Strange switch-case behaviour | ||||
|---|---|---|---|---|---|
| Submitted: | 26 Sep 2003 4:34am UTC | Modified: | 26 Sep 2003 6:15am UTC | ||
| From: | zeug at delirium dot ch | Assigned to: | |||
| Status: | Bogus | Category: | Documentation problem | ||
| Version: | Irrelevant | OS: | * | ||
[26 Sep 2003 4:34am UTC] zeug at delirium dot ch
[26 Sep 2003 4:45am UTC] sniper@php.net
Actually this is not bug. You can't have anything between
switch()..case.. ever. It's not valid.
This is same as what you tried:
<?php
$var = 1;
switch (1) {
echo " ";
case 1:
echo $var;
}
?>
[26 Sep 2003 4:52am UTC] zeug at delirium dot ch
Wow, that was fast :-) Why shouldn't you be allowed to have whitespace between the opening switch and the first case clause when it's okay to have whitespace between case clauses and the final case/default clause and endswitch - unless of cause eliminating this exception means messing up the parser code?
[26 Sep 2003 5:01am UTC] tony2001 at phpclub dot net
just don't open/close php-tags on every line of your script.
<?
switch () {
//as many empty lines as you want.
case 1:
break;
}
?>
[26 Sep 2003 5:18am UTC] zeug at delirium dot ch
Now, that's a matter of taste. I code 99% as classes in external files.
So only very little PHP remains in the actual page file, mainly to
arrange the HTML output. Picture this:
<html>
<?php
// here go a couple of PHP code using external
// classfiles that set a $return value
?>
* SOME HTML CODE *
<?php select ($return) ?>
<?php case 1: ?>
* A BUNCH OF HTML CODE *
<?php break ?>
<?php case 2: ?>
* OTHER HTML CODE *
<?php break ?>
...
<? endswitch ?>
* FINAL HTML CODE *
</html>
I currently omit the empty line between the switch and the first case,
yet why is one Newline after the swtich okay, but two Newlines fail
parsing?
[26 Sep 2003 5:24am UTC] tony2001 at phpclub dot net
as Wez already said, ---- <?php switch ($return) ?> <?php case 1: ?> ---- and ---- <?php switch ($return) case 1: ?> ---- are not the same things. first is equal to: <?php switch ($return): echo "\n"; case 1: ?> and this is invalid syntax.
[26 Sep 2003 6:05am UTC] zeug at delirium dot ch
Legalize it :-) After all, with <?php switch ($var): ?> <?php case 1: ?> there's also a newline finishing the switch-clause that's residing outside the PHP-block. It should be clear, that <?php switch ($var): ?> HTML blah blah <?php case 1: ?> is illegal. But I guess I grab the point: An empty line is considered an empty line of HTML rather than an empty line of PHP. So the parser strips whitespace AFTER a <?php ?> block, but doesn't ignore empty lines. This behaviour makes sense in most cases but maybe this switch-whitespace-case one. Never mind, it smells like tweaking the parser for no big issue, so adding a phrase to the docs will do I guess. Thanks for the fast replies, that's really cool!
[26 Sep 2003 6:15am UTC] goba@php.net
It is documented (http://php.net/language.basic-syntax) that <?php echo "abc"; ?> <?php echo "def"; ?> will print "abcdef" (without any space), as the closing newline after the ?> is ignored. If you put any HTML code between the two blocks, then it is not ignored. PHP does not mangle the contents of HTML blocks.
