开发者

PHP: Conditional statement with possible empty variables

开发者 https://www.devze.com 2023-01-09 23:29 出处:网络
I\'m creating a custom search form and when I try and sort the results I get all the objects displayed inste开发者_运维问答ad of the matched criteria. The reason I discovered was that some of the inpu

I'm creating a custom search form and when I try and sort the results I get all the objects displayed inste开发者_运维问答ad of the matched criteria. The reason I discovered was that some of the inputs from the form don't have a default value and when this is not declared in the conditional statement later on (for sorting) it just shows all the objects, whether the other requirements are met or not. I tried applying an OR statement with the specific variables able to be empty, but it gave the same result. Like so -

<?php if ($bedrooms >= $min_rooms 
          && $bedrooms <= $max_rooms 
          && $space >= $min_space 
          && $space <= $max_space 
          && $price >= $min_price 
          && $price <= $max_price 
          && $sel_type == $type 
          || $sel_type == '' 
          && $country == $sel_country 
          || $sel_country == '' ) { ?>

(See the last two statements) I was thinking of checking each variable in the conditional statement before including it but it feels like unnecessary code. How would you do it?


The && operator has a higher precedence than the || operator, so your expression is currently grouped like this, probably not what you want:

($bedrooms >= $min_rooms && $bedrooms <= $max_rooms && $space >= $min_space && $space <= $max_space && $price >= $min_price && $price <= $max_price && $sel_type == $type)
||
($sel_type == '' && $country == $sel_country)
||
($sel_country == '' )

Try adding parentheses like this to achieve correct grouping:

($bedrooms >= $min_rooms && $bedrooms <= $max_rooms && $space >= $min_space && $space <= $max_space && $price >= $min_price && $price <= $max_price && ($sel_type == $type || $sel_type == '') && ($country == $sel_country || $sel_country == '') )


Your expression may fail as the && operator has a higher precedence than the || operation. That means an expression like this:

… && $sel_type == $type || $sel_type == ''

is equivalent to this (operator precedence highlighted by using parentheses):

(… && $sel_type == $type) || $sel_type == ''

To fix that put the || expressions in parentheses:

$bedrooms >= $min_rooms && $bedrooms <= $max_rooms && $space >= $min_space && $space <= $max_space && $price >= $min_price && $price <= $max_price && ($sel_type == $type || $sel_type == '') && ($country == $sel_country || $sel_country == '')

Additionally, your expression is probably easier to read and to maintain if you use some helper functions like a between function:

function between($val, $min, $max) {
    return $min <= $val && $val <= $max;
}

Then your expression reads:

between($bedrooms, $min_rooms, $max_rooms) && between($space, $min_space, $max_space) && between($price, $min_price, $max_price) && ($sel_type == $type || $sel_type == '') && ($country == $sel_country || $sel_country == '')
0

精彩评论

暂无评论...
验证码 换一张
取 消