php 多个 if 语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10352161/
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 multiple if statements?
提问by patrick
This is going to seem like a noob question, sorry. I can't get my brain working this morning.
这看起来像是一个菜鸟问题,抱歉。今天早上我无法让我的大脑工作。
I am trying to perform multiple ifstatements, but they are not behaving properly. It appears to be always loading the least Template after it finds the one that it is looking for.
What is the best way to do something like this:
我正在尝试执行多个if语句,但它们的行为不正常。在找到它正在寻找的模板后,它似乎总是加载最少的模板。做这样的事情的最佳方法是什么:
$post = $wp_query->post;
if ( in_category('7') ) {include(TEMPLATEPATH . '/post-experts.php');}
if ( in_category('6') ) {include(TEMPLATEPATH . '/post-radio.php');}
if ( in_category('5') ) {include(TEMPLATEPATH . '/post-lifestyle.php');}
else {include(TEMPLATEPATH . '/singleorigional.php');
}
回答by Sam Sussman
You most likely want to do else iffor the 2nd and 3nd ifs or have a way to know if none of the are true, do the else statement
您最有可能想要else if为第二个和第三个 ifs 做或有办法知道是否都不是真的,请执行 else 语句
回答by cmprogram
For efficiency, you're best to use a switch statement, and then to catch those you have not found in your cases, you can use a default.
为了效率,你最好使用 switch 语句,然后捕捉那些你没有在你的案例中找到的,你可以使用默认值。
switch(in_category){ //Begin switch statement.
case '7': //Check if it equals 7
include(TEMPLATEPATH . '/post-experts.php'); //Include our PHP code
break; //End this current condition.
case '6': //Check if it equals 6
include(TEMPLATEPATH . '/post-radio.php'); //Include our PHP code
break; //End this current condition.
case '5': //Check if it equals 5
include(TEMPLATEPATH . '/post-lifestyle.php'); //Include our PHP code
break; //End this current condition.
default: //If none of the above cases are found, do this.
include(TEMPLATEPATH . '/singleorigional.php'); //Include our PHP code
break; //End this current condition.
}
Edit:I decided to return to this at a later date, to better explain why this is better.
编辑:我决定稍后再回到这里,以更好地解释为什么这更好。
An if else combination implies an order. E.g.
if else 组合意味着一个顺序。例如
if(thingy == "thing1"){
//Do one thing
}
elseif(thingy == "thing2"){
//Do another thing
}
elseif(thingy == "thing3"){
//Do a different thing
}
else{
//Catch anything
}
With this, it means it will check the first condition, if thing == thing1, if not, check if it equals the next condition which is thing == thing2 and so on. If you're generally always expecting thing1, then this might be okay, because you're just catching a few other things. However, realistically it's inefficient to check all possible conditions before you reach the solution you need.
有了这个,这意味着它将检查第一个条件,如果thing == thing1,如果不是,则检查它是否等于下一个条件,即thing == thing2,依此类推。如果您通常总是期待thing1,那么这可能没问题,因为您只是在捕捉其他一些东西。但是,实际上,在找到所需的解决方案之前检查所有可能的条件是低效的。
Instead, by writing the equivalent switch statement:
相反,通过编写等效的 switch 语句:
switch(thingy){
case "thing1":
//Do one thing
break;
case "thing2":
//Do another thing
break;
case "thing3":
//Do a different thing
break;
default:
//Catch anything
break; //Break is not needed if default is the final case.
}
What this does instead, is grab the answer first, e.g. thing == "thing3", and then it'll skip the other cases that are irrelevant, and instead only do what it needs to do. It does not use an order, instead it works a bit like pointing to the correct answer. So it doesn't matter if your actual answer is the first case, or the hundredth, it only does what is relevant.
相反,它的作用是首先获取答案,例如 thing == "thing3",然后它会跳过其他不相关的情况,而只做它需要做的事情。它不使用顺序,而是有点像指向正确答案。因此,您的实际答案是第一种情况还是第一种情况并不重要,它只会做相关的事情。
So to summarise: If you use a switch, and your answered case is the hundredth case, it'll point to what to do after that switch(answer) is found, if you were to use ifelse and your answer was the 100th variation of the ifelse, it would have to iterate through 99 other pointless checks before doing what it needs to do.
所以总结一下:如果您使用开关,并且您的回答案例是第 100 个案例,它将指出找到该开关(答案)后要做什么,如果您要使用 ifelse 并且您的答案是第 100 个变体ifelse,在做它需要做的事情之前,它必须遍历 99 个其他毫无意义的检查。
回答by bacardnumberone
i think you the problem is that the If statements are independent. Either if you have array with categories try use switch statement or if you have only in_category function which a think returns boolean then use elseif statement e.g.:
我认为你的问题是 If 语句是独立的。如果您有带有类别的数组,请尝试使用 switch 语句,或者如果您只有 in_category 函数,该函数会返回布尔值,然后使用 elseif 语句,例如:
if (in_category(7)){...}
elseif (in_category(6)){...}
elseif (in_category(5)){...}
else {
...
}
回答by Dave
My assumption is that whatever it is you're looking for "in_category" can be found in more than one category - hence not one long if block. Try this:
我的假设是,无论您在寻找什么“in_category”,都可以在不止一个类别中找到——因此不是一个很长的 if 块。尝试这个:
Condensed version:
精简版:
$post = $wp_query->post;
$found = false;
if ( in_category('7') ) { include(TEMPLATEPATH . '/post-experts.php'); $found = true; }
if ( in_category('6') ) {include(TEMPLATEPATH . '/post-radio.php'); $found = true; }
if ( in_category('5') ) {include(TEMPLATEPATH . '/post-lifestyle.php'); $found = true; }
if(!$found) include(TEMPLATEPATH . '/singleorigional.php');
More easy to read/understand version:
更易于阅读/理解的版本:
$post = $wp_query->post;
$found = false;
if ( in_category('7') ) {
include(TEMPLATEPATH . '/post-experts.php');
$found = true;
}
if ( in_category('6') ) {
include(TEMPLATEPATH . '/post-radio.php');
$found = true;
}
if ( in_category('5') ) {
include(TEMPLATEPATH . '/post-lifestyle.php');
$found = true;
}
if(!$found) include(TEMPLATEPATH . '/singleorigional.php');
OR - if it can only be found in one category:
或者 - 如果它只能在一个类别中找到:
$post = $wp_query->post;
if ( in_category('7') ) {
include(TEMPLATEPATH . '/post-experts.php');
} else if ( in_category('6') ) {
include(TEMPLATEPATH . '/post-radio.php');
} else if ( in_category('5') ) {
include(TEMPLATEPATH . '/post-lifestyle.php');
} else {
include(TEMPLATEPATH . '/singleorigional.php');
}
回答by nyson
As @thantos said, include else if statements.
正如@thantos 所说,包括 else if 语句。
EDIT: WordPresstells me it needs a second argument to the in_category function, namely the $post variable; I edited my response to match that.
编辑:WordPress告诉我它需要 in_category 函数的第二个参数,即 $post 变量;我编辑了我的回复以匹配它。
Example:
例子:
$post = $wp_query->post;
if (in_category('7', $post)) {
include(TEMPLATEPATH . '/post-experts.php');
}
else if (in_category('6', $post)) {
include(TEMPLATEPATH . '/post-radio.php');
}
else if (in_category('5', $post)) {
include(TEMPLATEPATH . '/post-lifestyle.php');
}
else {
include(TEMPLATEPATH . '/singleorigional.php');
}

