3

I have a php file in which i am using a really very long switch case. I want to split the cases in different files (keep logically connected cases in 1 file).

EDIT: Sorry everyone it was my code that was causing problem. The switch case was working as expected.

file -> a.php

echo "<br>RES = ".test(1);

function test($value) {
    switch($value) {
        case (1 || 2):
                include("b.php");
                            **return $temp;**
                break;

        default: echo "error";
                return 3;
                break;
    }
}

file -> b.php

switch($value) {

    case 1: echo "value is 1";
                    **$temp = 1;**
            return 1;
            break;

    case 2: echo "value is 2";
                    **$temp = 2;**
                    return 2;
                    break;
}

How do i get proper result? if the switch case of b.php is in a.php file then everything works fine.Any idea/suggestion on how to do this?

If i add $temp (bold lines) then it works...

Thanks for help in advance.

Regards

user427969
  • 3,836
  • 6
  • 50
  • 75

3 Answers3

4

Updated response to updated question: modify "a.php" and prefix a return infront of the "b.php" include:

return include("b.php");

http://www.php.net/manual/en/function.include.php

Handling Returns: It is possible to execute a return() statement inside an included file in order to terminate processing in that file and return to the script which called it. Also, it's possible to return values from included files. You can take the value of the include call as you would a normal function. This is not, however, possible when including remote files unless the output of the remote file has valid PHP start and end tags (as with any local file). You can declare the needed variables within those tags and they will be introduced at whichever point the file was included.


simple include()'s within your case/break sections?

switch($var)
{
 case 1:
   include('case_1.php');
   break;
 case 2:
   include('case_2.php');
   break;
 default:
   include('case_default.php');
  break;
}
Scuzzy
  • 12,186
  • 1
  • 46
  • 46
  • hi thanks for replying. I tried that but it didn't work. I have 5 cases in a file then how do i do it? Regards – user427969 Jul 01 '11 at 02:56
  • Extrapolate on my example, since you didn't post any code examples I just gave you a generic structure for you to massage into your own system. use a different $var, if you want to match on strings use case "foo": – Scuzzy Jul 01 '11 at 03:51
  • Hi thanks for replying. I have editted my question to give some idea. Sorry initially i thought switch case is causing problem but its not like that. Regards – user427969 Jul 01 '11 at 04:37
  • See now that's interesting, that looks like a bug in that the included file has some how lost that it's within a function and the return isn't behaving like expected. – Scuzzy Jul 01 '11 at 04:50
  • Exactly... any idea how to overcome this? – user427969 Jul 01 '11 at 04:54
  • as goofy as this sounds, try using... return include("b.php"); – Scuzzy Jul 01 '11 at 04:59
0

This is actually something that Scuzzy proposed (I have even left the same naming convention), but improved:

// list of files
$my_files = array(
    'a' => 'case_1.php',
    'b' => 'case_2.php',
    'c' => 'case_3.php',
    'd' => 'case_4.php',
);

// determine which one to load
if (array_key_exists($var, $my_files)) {
    include($my_files[$var]);
} else {
    include('case_default.php');
}

or even shorter ;) :

$f = array('a','b','c','d');
include((in_array($var,$f)?$var:'case_default').'.php');
Tadeck
  • 132,510
  • 28
  • 152
  • 198
0
switch($var)
{
    case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: //...
        include('case_' . $var . '.php');
        break;
    default:
        include('case_default.php');
        break;
}
maaudet
  • 2,338
  • 4
  • 20
  • 28