php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #18483 include fails in a switch context
Submitted: 2002-07-22 22:46 UTC Modified: 2002-07-23 08:59 UTC
From: nielsene at mit dot edu Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 4.1.2 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: nielsene at mit dot edu
New email:
PHP Version: OS:

 

 [2002-07-22 22:46 UTC] nielsene at mit dot edu
Using an include (or require) from within a switch statement, generates a parse error (expecting T_CASE or T_DEFAULT).

--test.php
<?php
$foo=1;
switch($foo)
{
include "bar.inc";
}
exit;
?>
--bar.inc
<?php
case 1: echo "1"; break;
case 2: echo "2"; break;
default : echo "3"; break;
?>
--

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-07-23 01:37 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php
 [2002-07-23 08:47 UTC] nielsene at mit dot edu
I'm sorry, I don't see how this is not a bug.  I checked over the documentation included user comments before submitting this.  No where does it mention that includes are not valid within a switch context.  It does mention the need for the braces within a conditional context, which led me to beleive it should work within a switch.  If the parser is behaving correctly, then this might be a documentation oversight.
 [2002-07-23 08:50 UTC] derick@php.net
it has nothing to do with the include statement, but with the switch() statement. Switch() just expects "case " or "default" there. I'm fairly sure this works:

switch ($foo) {
    case 1: include "bar.inc"; break;
}

It's just not supported.


Derick
 [2002-07-23 08:54 UTC] nielsene at mit dot edu
I'll change this from a bug report to a feature request as it appears that this is an unsupported use combination of switch/include.

I'm writing a family of closely related websites.  Costumization code is written by a configurer for including at various points in the boiler-plate code.  In several locations this customization code is the body of the switch, with the default being constant between sites, so the include file is just a list of case statements.
 [2002-07-23 08:59 UTC] derick@php.net
Why don't you include the switch() itself in the include file?

Derick
 [2002-07-23 09:15 UTC] nielsene at mit dot edu
Since you ask,

I'm trying to keep the include files as simple and short as possible.  First, I find it helps readability to make the main boiler-plate code as completed as possible.  Second,I'm trying to keep the automated code generation code simple.  In many cases I'm outputing code that when run will then output html.  At all points along the way I want the first, and second generation outputs to be well formated.  As a result the code generation is already a nightmare of singly and doubly escaped control characters so that they (tabs/newlines) get passed through to the different levels.

I am going to move the switch to the include files as its the only workaround availible at present.  I had fallen back into the trap of thinking of include() like a preprocessor-like command.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Fri Jun 19 11:00:01 2026 UTC