PHP Bugs  
php.net | support | documentation | report a bug | advanced search | search howto | statistics | login

go to bug id or search bugs for  

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:*
View/Vote Developer Edit Submission

Welcome! If you don't have a SVN account, you can't do anything here. You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
SVN Username: SVN Password:
Quick Fix:
Status: Assign to:
Category:
Summary:
From: zeug at delirium dot ch
New email:
Version: OS:
New/Additional Comment:

[26 Sep 2003 4:34am UTC] zeug at delirium dot ch
Description:
------------
Hi there

This is a very minor bug concerning the structure of a source code
rather than its functionality. It's been dealt with in Bug #13387, yet
the case was closed back then when it seems to have been fixed in
4.2.0-dev. It's back in 4.3.3.

Here are some examples:

Example 1, okay:
<?php switch ($var): ?>
<?php case 1: ?>
<?php dothis() ?>
<?php endswitch ?>

Example 2, okay:
<?php switch ($var) { ?>

<?php case 1: ?>
<?php dothis() ?>

<?php } ?>

Example 3, syntax error:
<?php switch ($var): ?>

<?php case 1: ?>
<?php dothis() ?>

<?php endswitch ?>

The parser doesn't seem to like whitespace in HTML between switch and
case.

When mixing PHP and HTML code, the syntax in example 3 can improve
readablility. 

Reproduce code:
---------------
<?php $var = 1 ?>
<?php switch ($var): ?>

<?php case 1: ?>
<?php print $var ?>

<?php endswitch ?>

Expected result:
----------------
1

Actual result:
--------------
Parse error: parse error, expecting `T_ENDSWITCH' or `T_CASE' or
`T_DEFAULT' in .../test.php on line 4
[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.

RSS feed | show source 

PHP Copyright © 2001-2009 The PHP Group
All rights reserved.
Last updated: Sat Nov 21 10:30:49 2009 UTC