php - 将开关案例拆分到不同的文件中
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对更新问题的更新回复:
修改“a.php”并在“b.php”前面添加 return 前缀,包括:
http://www.php.net/manual/en/function.include.php
简单的 include() 在你的 case/break 部分中?
Updated response to updated question:
modify "a.php" and prefix a return infront of the "b.php" include:
http://www.php.net/manual/en/function.include.php
simple include()'s within your case/break sections?
这实际上是 Scuzzy 提出的(我什至保留了相同的命名约定),但进行了改进:
甚至更短;):
This is actually something that Scuzzy proposed (I have even left the same naming convention), but improved:
or even shorter ;) :