Szymon Toda
Szymon Toda

Reputation: 4516

my "if" statement returns false even if its true, why?

$status returns 0, but loop acts like its "1". Funny thing here is my error message is "MySQL DB is in use (status 0)".

In other words: my if statement fails even if its true.

Where is mistake?

include 'status_check.php'; //it takes $check from MySQL DB
if($status = "0") { //if status is 0 go on
    include 'status_1.php'; //set status to 1
            ...
    include 'status_0.php'; //after finished operation set status back to 0
} else { //if status is 1 say that its 1
    echo "MySQL DB is in use (status ". $status .")";
    die;
}

Upvotes: 0

Views: 359

Answers (1)

Wiseguy
Wiseguy

Reputation: 20873

You're assigning, not comparing.

if($status = "0") {

Make that == for a comparison.

It's thus false because the string "0" is treated as a "falsy" value in PHP. Docs: http://php.net/boolean#language.types.boolean.casting

Upvotes: 17

Related Questions