例如,在这样的PHP代码中:
if($this->function1() && $this->function2())
{
//everything is alright
}
else
{
//function1 or function2 returned false
//whodunnit?
}
Run Code Online (Sandbox Code Playgroud)
是否有语言结构或某些东西可以帮助我找出else块中的罪魁祸首?我不想再次运行条件,只是为了找出问题所在.
请忍受我的伪代码.我的实际代码在这里看起来完全脱离了上下文...顺便说一句,我正在使用CodeIgniter.
if($this->function1()) {
if($this->function2()) {
// both worked
} else {
// function1 returned a true value
// function2 returned a false value
}
} else {
// function1 returned a false value
// (you could optionally call $this->function2() here)
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您总是想要运行这两个功能:
$result1 = $this->function1();
$result2 = $this->function2();
if($result1 && $result2) {
// everything is okay
} else {
// look at the results to figure out what to do
}
Run Code Online (Sandbox Code Playgroud)
或者,如果你想做第一个但没有2个级别的缩进:
if(!$this->function1()) {
// function1 returned false
// (you can call $this->function2() here if you want)
} elseif (!$this->function2()) {
// function2 returned false
} else {
// everything is okay
}
Run Code Online (Sandbox Code Playgroud)