我正在尝试更好地组织我的“行动”,目前这是一个包含大量案例的巨型转换声明,并且很难管理。我想将操作移动到可以更轻松管理的文件中。但我正试图解决一个问题。
我有一个foreach循环遍历所有“被调用的动作”并调用它们。然后我有一堆动作,但是我希望结束当前循环的执行(即continue;
或break;
),但这似乎不适用于包含的文件。
我还能做到这一点吗?我还需要“操作”才能访问执行脚本中定义的所有当前变量(这就是我选择包含的原因)。
目前...
included_file.php
<?php
blah blah stuff
if(statement) {
// accesses variables declared in calling_file.php
continue;
}
?>
calling_file.php
<?php
blah blah stuff
// declare variables that need to be accessed in included_files.php
foreach() {
include included_file.php
}
?>
现在我想停止当前循环并转到下一个循环。有什么想法吗?
答案 0 :(得分:0)
如果我正确理解了您的问题,您似乎希望从包含的文件中获取结果,该文件表明您要执行的操作(break
,continue
),然后使用简单的switch语句内循环到break
外循环。 break control structure将允许您执行此操作。
<强> calling_file.php 强>
<?php
$includes = array ('included_file1.php', 'included_file2.php', 'included_file3.php');
const CONTROL_BREAK = 3;
const CONTROL_CONTINUE = 7;
// declare variables that need to be accessed in included_files.php
foreach($includes as $include) {
print "Including $include\n";
$result = include($include);
switch ($result){
case CONTROL_BREAK:
break 2;
case CONTROL_CONTINUE:
default:
continue 2;
}
}
<强> included_file1.php 强>
<?php
print __FILE__ . " has been included!\n";
if(TRUE) {
print "I should continue!\n";
return CONTROL_CONTINUE;
}
<强> included_file2.php 强>
<?php
print __FILE__ . " has been included!\n";
if(TRUE) {
print "I should break!\n";
return CONTROL_BREAK;
}
<强> included_file3.php 强>
<?php
print __FILE__ . " has been included!\n";
if(TRUE) {
print "You should never see me!";
}