Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php multiple if statements?

Tags:

php

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 if statements, 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:

$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');
}

example

like image 496
patrick Avatar asked Nov 28 '22 17:11

patrick


2 Answers

You most likely want to do else if for the 2nd and 3nd ifs or have a way to know if none of the are true, do the else statement

like image 93
Sam Sussman Avatar answered Dec 10 '22 11:12

Sam Sussman


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(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(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.

Instead, by writing the equivalent switch statement:

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.

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.

like image 43
cmprogram Avatar answered Dec 10 '22 11:12

cmprogram