PHP - 嵌套的 IF 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11839268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - Nested IF statements
提问by Kausheel
I'm wondering when it's a bad ideato use multiple nested IF statements.
我想知道什么时候使用多个嵌套的 IF 语句是个坏主意。
eg:
例如:
function change_password($email, $password, $new_password, $confirm_new_password)
{
if($email && $password && $new_password && $confirm_new_password)
{
if($new_password == $confirm_new_password)
{
if(login($email, $password))
{
if(set_password($email, $new_password))
{
return TRUE;
}
}
}
}
}
This function is used like this:
这个函数是这样使用的:
if(!change_password($email, $password, $new_password, $confirm_new_password)
{
echo 'The form was not filled in correctly!';
exit;
}
I call all my functions like this, and I'm wondering if there's something wrong with my coding style. I'm having my doubts because if I follow this design then that means every single function I write will just be with nested with IF's, checking if there are errors at every stage. Is this what other people do?
我这样称呼我的所有函数,我想知道我的编码风格是否有问题。我有我的疑问,因为如果我遵循这种设计,那么这意味着我编写的每个函数都将与 IF 嵌套,检查每个阶段是否存在错误。这是其他人的做法吗?
I don't see many other scripts written like this, with the nested IF's making a triangle shape and only having the desired result in the very middle. If the middle isn't reached, then something screwed up.
我没有看到像这样编写的许多其他脚本,嵌套的 IF 形成一个三角形,并且只在中间得到所需的结果。如果没有达到中间,那么事情就搞砸了。
Is this a good function structure?
这是一个好的函数结构吗?
回答by Marc B
Nesting too deeply is generally a bad idea - it's spaghetti logic and difficult to follow. Since each of your verification steps depends on the previous stage having succeeded, don't nest at all - just bail out when a stage fails:
嵌套太深通常是一个坏主意 - 这是意大利面条式的逻辑并且难以遵循。由于您的每个验证步骤都取决于前一个阶段是否成功,因此根本不要嵌套 - 只是在某个阶段失败时退出:
function change_password(blah blah blah) {
if (!$condition1) {
return false;
}
if (!$condition2) {
return false;
}
etc....
// got here, must have succeeded
return true;
}
That makes it explicitly clear what the logic sequence is.
这使得逻辑顺序是明确的。
回答by Christian Schnorr
I think it is definitely well readable and can easily be understood in comparison to using just one ifstatement like
我认为它绝对可读性强,并且与仅使用一个if语句相比很容易理解
if (blah and blah and blah and blah and blah and blah and blah) {}
However I'd still prefer doing it this way - too much indention can get kinda annoying:
但是我仍然更喜欢这样做 - 太多的缩进会有点烦人:
function change_password($email, $password, $new_password, $confirm_new_password)
{
if (!$email || !$password || !$new_password || !$confirm_new_password) return false;
if ($new_password != $confirm_new_password) return false;
if (!login($email, $password)) return false;
if (!set_password($email, $new_password)) return false;
return true;
}
回答by Brett Thomas
It can be good to nest them, because by changing the order you may be able to avoid making extra comparisons. What you are doing now looks good, however your function would be less efficient if you instead wrote it as:
嵌套它们可能会很好,因为通过更改顺序,您可以避免进行额外的比较。您现在所做的看起来不错,但是如果您改为将其编写为:
function change_password($email, $password, $new_password, $confirm_new_password)
{
if($new_password == $confirm_new_password && $email && $password && $new_password && $confirm_new_password)
{
if(login($email, $password))
{
if(set_password($email, $new_password))
{
return TRUE;
}
}
}
}
If $new_password == $confirm_new_password is true, but $email is empty, you will have made an extra comparison.
如果 $new_password == $confirm_new_password 为真,但 $email 为空,您将进行额外的比较。
As others have said, there are other ways to go about this without nesting everything, which will be functionally equivalent.
正如其他人所说,还有其他方法可以在不嵌套所有内容的情况下解决此问题,这在功能上是等效的。

