简体   繁体   中英

PHP if not doesn't seem to work right

This doesn't seem to work, don't know why but the IF NOT statement doesn't seem to work out for me. I dobble checked everything and it's spelled right.

This first piece of code seems to work fine.

$queryString = "SELECT * FROM biler";   

if($searchType == "alle") {
    //nothing
} else {
    $queryString = " WHERE bilType = '$searchType'";
}

But this doesn't.

$queryString = "SELECT * FROM biler";   

if(!$searchType == "alle") {
    $queryString = " WHERE bilType = '$searchType'";
}

Your "!" is in the wrong place

Try this:

if($searchType != "alle") {
    $queryString = " WHERE bilType = '$searchType'";
}

!= stands for "does not equal"

What you were trying to do with your code was ask if the variable is not "true"

You are incorrectly comparing. Should be

$queryString = "SELECT * FROM biler";   

if($searchType != "alle") {
    $queryString .= " WHERE bilType = '$searchType'";
}

see php Comparison Operators - http://php.net/ternary

Two things...

Frist, those are entirely opposite conditions. So even if the second one did "work" it would still produce the opposite effect of the first one.

Second, the operators don't work in the order you think they do. You're essentially doing this:

if ((!$searchType) == "alle")

You can use this:

if (!($searchType == "alle"))

Or, more cleanly:

if ($searchType != "alle")

Your ! should be applied on whole condition ie $searchType == "alle" like below

$queryString = "SELECT * FROM biler";   

if(!($searchType == "alle")) {
    $queryString = " WHERE bilType = '$searchType'";
}

Have you checked $searchType ? Maybe it doesn't exist? Example:

$queryString = "SELECT * FROM biler";   

if(isset($searchType) && $searchType == "alle") {
    //nothing
} else {
    $queryString = " WHERE bilType = '$searchType'";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM