Confusing IF clauses
I'm making a search engine, and I've made it so that a query is put together from the different types of search you want to do (e.g. Username, City, Age etc.). I've come to the part where I have to do IF statements to output the correct results, but for some reason I can't figure out how it selects another IF statement than I'd want it to.
The script echoes out which block of IF has been executed for debugging, and it says that the "age" block is executed instead of the "county" which I want. Here's the code, and for the record; the NORMAL and AGE ifs work just fine, so I suspect a syntax error I can't see. Am I doing something wrong logic wise?
First IF
if(($searchFromAge == null || $searchFromAge == "noOption") && (开发者_Python百科$searchToAge == null || $searchToAge == "noOption") && ($searchCounty == null || $searchCounty == "noOption") && ($searchCity == null || $searchCity == "By")){
echo "normal";
}
Age IF
elseif(($searchFromAge != null || $searchFromAge != "noOption") && ($searchToAge != null || $searchToAge != "noOption")){
echo "age";
}
County IF
} elseif($searchCounty != null || $searchCounty != "noOption"){
echo "county";
}
Script output: age
$searchFromAge != null || $searchFromAge != "noOption"
this condition will be true if $searchFromAge == "noOption" because it is != null.
Tou need to make changes in condition.
Use
($searchFromAge != null && $searchFromAge != "noOption")
in if condition.
It is same for all your conditions. use &&
instead of ||
You did your logical negations wrong. The negation of (A == B || C == D)
is (A != B && C != D)
, noticing the &&
.
So:
$searchFromAge != null || $searchFromAge != "noOption"
becomes:
$searchFromAge != null && $searchFromAge != "noOption"
Hope that helps.
精彩评论