I was thinking about this some time. I'm using a lot if statements, and sometimes I have a lot of them. I know I can use switch
to shorten a bit the code. Is there another way to shorten them?
Let's say we have this example:
$all_conditions = '';
if($post_status != ""){
$conditions[] = "posts.post_status = '".$post_status."'";
}
if($post_type != ""){
$conditions[] = "posts.post_type = '".$post_type."'";
}
if($meta_key != ""){
$conditions[] = "postmeta.meta_key = '".$meta_key."'";
}
if($meta_value != ""){
$conditions[] = "postmeta.meta_value = '".$meta_value."'";
}
if($date_begin != "" && $date_end != ""){
$conditions[] = "( posts.post_date BETWEEN '".$date_begin."' AND '".$date_end."' )";
}
if(count($conditions)>0){
$all_conditions = implode(" AND ",$conditions);
}
if($all_conditions != ""){
$all_conditions = "WHERE ".$all_conditions;
}
What should I do to not repeat all those if's?