$foo=1;
function someFunction(){
if($foo==0){ //-------Will test, won't execute
bar();
}elseif($foo==1){ //--Will test, and execute
baz();
}elseif($foo==2){ //--Doesn't test
qux();
}elseif($foo==3){ //--Doesn't test
quux();
}else{ //-------------Doesn't test
death();
} //------------------The program will skip down to here.
}
Run Code Online (Sandbox Code Playgroud)
假设baz()改变了$ foo的值,每次都不同.我希望我的代码在第一个之后继续测试elseif/else语句,如果它们是真的则运行它们.
我不想再次运行整个函数,(即我不关心$ foo = 0还是1).我正在寻找像"继续"这样的东西.无论如何,请告诉我这是否可行.谢谢.:)
编辑**我的代码实际上比这更复杂.我只是为了理论而放下一些代码.我想要的只是脚本继续测试它通常不会的地方.
如果我理解正确的话,你想要做的每一个连续的elseif,不管以前是否if/ elseif小号匹配的,但你也想,如果一些代码运行没有了的if/ elseif的比赛.在这种情况下,您可以设置一个标志$matched,true如果其中一个匹配并使用ifs代替.
<?php
$foo=1;
function someFunction(){
$matched = false;
if($foo==0){
bar();
$matched = true;
}
if($foo==1){ //--This elseif will get executed, and after it's executed,
baz();
$matched = true;
}
if($foo==2){
qux();
$matched = true;
}
if($foo==3){
quux();
$matched = true;
}
if(!$matched){ /* Only run if nothing matched */
death();
}
}
Run Code Online (Sandbox Code Playgroud)
<?php
$foo=1;
function someFunction(){
$matched = false;
if($foo==0){
bar();
$matched = true;
goto end: // Skip to end
}
if($foo==1){ //--This elseif will get executed, and after it's executed,
baz();
$matched = true;
}
if($foo==2){
qux();
$matched = true;
}
if($foo==3){
quux();
$matched = true;
}
if(!$matched){ /* Only run if nothing matched */
death();
}
end:
}
Run Code Online (Sandbox Code Playgroud)