缩小一堆“if”,“elseif”。 (16种可能性)

时间:2013-08-08 12:47:49

标签: php function optimization if-statement reduce

我已经想过如何让托管更容易/更清洁/更好(我想我的方法不是服务器友好的,虽然我不确定)。

我的目的:减少(如果可能)“if”部分。该部分检查哪些下拉框已从“空”更改(它实际上是“全部”但如果它更适合某些内容则可以更改为空),并将其应用于仅显示满足这些条件的用户(家庭大学,主持人)大学,地点和/或国籍。这是我能够创造的唯一简单/基本方式。

我有这个功能:

function get_user_listing($curauth) {
  global $post;
  $concat = wpu_concat_single();
  // These get the values from the plugin Cimy User Extra Fields:
  $homeuni=get_cimyFieldValue($curauth->ID,'homeuni');
  $hostuni=get_cimyFieldValue($curauth->ID,'hostuni');
  $location=get_cimyFieldValue($curauth->ID,'location');
  $nationality=get_cimyFieldValue($curauth->ID,'nationality');
  // These get the values from a dropdown form in the page:
  $selectedhomeuni = $_POST['homeuni'];
  $selectedhostuni = $_POST['hostuni'];  
  $selectedlocation = $_POST['location'];
  $selectednationality = $_POST['nationality'];

//This is the code that has to be run every time to display every user info:
include '/home/u548205287/public_html/wp-content/themes/Trim/profilescode.php';

// I set an initial page that runs the code with no conditions because with 
the form, the page would look empty until the form is submitted once:
if(is_page(806)) {return $html;} 

else{
if($selectedhomeuni == "all" && $selectedhostuni == "all" && $selectedlocation == "all" && $selectednationality == "all") {return $html;} // The possibilities with each dropdown start here. If "all" (the "empty" one) is selected, nothing changes and all are displayed.
elseif($selectedhomeuni != "all" && $selectedhostuni == "all" && $selectedlocation == "all" && $selectednationality == "all") {if($homeuni==$selectedhomeuni) {return $html;}}  // If any dropdown is selected, its value acts as a filter and only the users with that info are shown.
elseif($selectedhomeuni == "all" && $selectedhostuni != "all" && $selectedlocation == "all" && $selectednationality == "all") {if($hostuni==$selectedhostuni) {return $html;}}
elseif($selectedhomeuni == "all" && $selectedhostuni == "all" && $selectedlocation != "all" && $selectednationality == "all") {if($location==$selectedlocation) {return $html;}}
elseif($selectedhomeuni == "all" && $selectedhostuni == "all" && $selectedlocation == "all" && $selectednationality != "all") {if($nationality==$selectednationality) {return $html;}}
elseif($selectedhomeuni != "all" && $selectedhostuni != "all" && $selectedlocation == "all" && $selectednationality == "all") {if($homeuni==$selectedhomeuni && $hostuni==$selectedhostuni) {return $html;}}
elseif($selectedhomeuni != "all" && $selectedhostuni == "all" && $selectedlocation != "all" && $selectednationality == "all") {if($homeuni==$selectedhomeuni && $location==$selectedlocation) {return $html;}}
elseif($selectedhomeuni != "all" && $selectedhostuni == "all" && $selectedlocation == "all" && $selectednationality != "all") {if($homeuni==$selectedhomeuni && $nationality==$selectednationality) {return $html;}}
elseif($selectedhomeuni != "all" && $selectedhostuni != "all" && $selectedlocation != "all" && $selectednationality == "all") {if($homeuni==$selectedhomeuni && $hostuni==$selectedhostuni && $location==$selectedlocation) {return $html;}}
elseif($selectedhomeuni != "all" && $selectedhostuni != "all" && $selectedlocation == "all" && $selectednationality != "all") {if($homeuni==$selectedhomeuni && $hostuni==$selectedhostuni && $nationality==$selectednationality) {return $html;}}
elseif($selectedhomeuni != "all" && $selectedhostuni == "all" && $selectedlocation != "all" && $selectednationality != "all") {if($homeuni==$selectedhomeuni && $location==$selectedlocation && $nationality==$selectednationality) {return $html;}}
elseif($selectedhomeuni != "all" && $selectedhostuni != "all" && $selectedlocation != "all" && $selectednationality != "all") {if($homeuni==$selectedhomeuni && $hostuni==$selectedhostuni && $location==$selectedlocation && $nationality==$selectednationality) {return $html;}}
elseif($selectedhomeuni == "all" && $selectedhostuni != "all" && $selectedlocation != "all" && $selectednationality == "all") {if($hostuni==$selectedhostuni && $location==$selectedlocation) {return $html;}}
elseif($selectedhomeuni == "all" && $selectedhostuni != "all" && $selectedlocation == "all" && $selectednationality != "all") {if($hostuni==$selectedhostuni && $nationality==$selectednationality) {return $html;}}
elseif($selectedhomeuni == "all" && $selectedhostuni != "all" && $selectedlocation != "all" && $selectednationality != "all") {if($hostuni==$selectedhostuni && $location==$selectedlocation && $nationality==$selectednationality) {return $html;}}
elseif($selectedhomeuni == "all" && $selectedhostuni == "all" && $selectedlocation != "all" && $selectednationality != "all") {if($location==$selectedlocation && $nationality==$selectednationality) {return $html;}}
}
} 

我想知道是否有更好的方法来做所有的if。我确定有。谢谢:))

1 个答案:

答案 0 :(得分:2)

你可以说出你的意思:

if(  ($selectedhomeuni == "all" || $selectedhomeuni == $homeuni)
  && ($selectedhostuni == "all" || $selectedhostuni == $hostuni)
  && ($selectedlocation == "all" || $selectedlocation == $location)
  && ($selectednationality == "all" || $selectednationality == $nationality)
  )
{
  return $html;
}