php - 将开关案例拆分到不同的文件中

发布于 2024-11-18 12:03:09 字数 904 浏览 0 评论 0原文

我有一个 php 文件,其中使用了一个非常长的 switch case。我想将案例拆分到不同的文件中(将逻辑连接的案例保留在 1 个文件中)。

编辑:对不起大家,是我的代码引起了问题。开关盒按预期工作。

文件-> 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;
    }
}

文件-> 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;
}

如何获得正确的结果?如果 b.php 的 switch case 位于 a.php 文件中,那么一切正常。关于如何执行此操作有任何想法/建议吗?

如果我添加 $temp (粗线),那么它就可以工作...

提前感谢您的帮助。

问候

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

楠木可依 2024-11-25 12:03:09

对更新问题的更新回复:
修改“a.php”并在“b.php”前面添加 return 前缀,包括:

return include("b.php");

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

处理退货:可以
在一个内部执行 return() 语句
包含文件以终止
处理该文件并返回
调用它的脚本。还有,它是
可以返回值
包含的文件。你可以取值
的 include 调用就像您一样
功能正常。然而,这并不是
包含远程文件时可能
除非远程文件的输出
具有有效的 PHP 开始和结束标记(如
与任何本地文件)。您可以声明
这些标签中所需的变量
他们将在以下时间介绍:
无论文件包含在哪一点。


简单的 include() 在你的 case/break 部分中?

switch($var)
{
 case 1:
   include('case_1.php');
   break;
 case 2:
   include('case_2.php');
   break;
 default:
   include('case_default.php');
  break;
}

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;
}
凑诗 2024-11-25 12:03:09

这实际上是 Scuzzy 提出的(我什至保留了相同的命名约定),但进行了改进:

// 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');
}

甚至更短;):

$f = array('a','b','c','d');
include((in_array($var,$f)?$var:'case_default').'.php');

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');
£烟消云散 2024-11-25 12:03:09
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;
}
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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文