Tutorial :PHP: Conditional statement with possible empty variables



Question:

I'm creating a custom search form and when I try and sort the results I get all the objects displayed instead 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?


Solution:1

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 == '') )  


Solution:2

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 == '')  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »