My code looks like this:
$muted = 'true'; //Note this is only for testing
if ($muted == 'false' || $muted == '' || $do_not_text == '' || $do_not_text =='false'){
//do this first thing
}
else{
//do something else
}
I can't get my else
to run. What syntax am I messing up?
Related to this, I'm storing a value in my database (that will be what's called to set $muted
in my real code) that's either 'true', 'false', or ''. What data type should I be storing these as? Currently, I'm using VARCHAR
, but I suspect this is all part of the problem.
$do_not_text == ''
evaluates to true. Why? Because $do_not_text
is not defined which is a falsy value. You are comparing it to an empty string which also equates to a falsy value. So that comparison is true causing the first if statement to be evaluated as true.
I'm not sure why you're using strings for what should be boolean values.
$muted = true; // no need for quotes
You might also consider using the ===
operator when comparing boolean values.
if ($muted === false || // etc...
What data type should I be storing these as?
Boolean values in MySQL are typically stored as 1 or 0.
field_name TINYINT(1) UNSIGNED NOT NULL DEFAULT 0
Store them as TINYINT
with length of 1
because its only 1 and 0, and 0
as the default value.
Then you can make $muted = boolval($db_muted_val);
if you want, or use $db_muted_val
as is, because 1
is true
and 0
is false
.
if ($db_muted_val) {
// do this first thing
} else {
// do something else
}
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.