php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #25667 Strange switch-case behaviour
Submitted: 2003-09-26 04:34 UTC Modified: 2003-09-26 06:15 UTC
From: zeug at delirium dot ch Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git 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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: zeug at delirium dot ch
New email:
PHP Version: OS:

 

 [2003-09-26 04:34 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-09-26 04:45 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;
}
?>
 [2003-09-26 04:52 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?
 [2003-09-26 05:01 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;
}
?>
 [2003-09-26 05:18 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?
 [2003-09-26 05:24 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.
 [2003-09-26 06:05 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!
 [2003-09-26 06:15 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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 01:01:28 2024 UTC