PHP-在不同文件中拆分开关盒
作者:互联网
我有一个php文件,其中我使用的是非常长的开关盒.我想将案例拆分为不同的文件(将逻辑连接的案例保留在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的切换大小写在a.php文件中,则一切正常.有关如何执行此操作的任何想法/建议?
如果我添加$temp(粗体行),那么它可以工作…
预先感谢您的帮助.
问候
解决方法:
对更新问题的更新回复:
修改“ a.php”并在“ b.php”的返回前缀之前添加前缀:
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.
您的案例/中断部分中是否包含简单的include()?
switch($var)
{
case 1:
include('case_1.php');
break;
case 2:
include('case_2.php');
break;
default:
include('case_default.php');
break;
}
标签:switch-statement,php 来源: https://codeday.me/bug/20191102/1991596.html