tags:

views:

89

answers:

3

I have a question regarding bools in php. I have a stored mysql proc that is returning a boolean. When this value is grabbed on the php side it displays the value as being a 0 or 1. This all seems fine to me and I have read in the php manual that php will interpret a 0 or 1 as false or true at compile time but this does not seem to be the case to me. I have gone a step further and casted my returned value with (bool) but this still does not seem to work.

My if statements are not properly firing because of this. Does anyone know what is going on? Thanks for the help.

+1  A: 

It sounds like the boolean value is being returned as a string.

Try something like this:

$your_bool = $field_value === "0" ? false : true;
Andrew Hare
`===` (triple-equals)? I think you mean `==`. Plus, it's better written as `$your_bool = ($field_value != "0");`.
amphetamachine
`$your_bool = (bool)$field_value` is more clear and it does the same thing.
ryeguy
`$your_bool = ($field_value !== "0")` - Judean Anti Ternary Operator Abuse Squad
deceze
@amphetamachine PHP does not know about MySQL types, so it will convert everything into a string.
Ionuț G. Stan
Tripple equal signs test with strong typing.
troelskn
@Ionut what's your point?
ryeguy
@amphetamachine: yes === exists and its different than ==. I find his statement to be more readable
Galen
@ryeguy my point is that `===` is valid in this answer.
Ionuț G. Stan
@troelskn - Ah, sorry. Didn't know that bit of PHP trivia.
amphetamachine
@Ionut yes it will work, but it's unnecessary.
ryeguy
+2  A: 

MySQL does not have a proper BOOL or BOOLEAN data types. They are declared as synonyms for TINYINT(1). Your procedure will return 0 or 1, which being on non-PHP ground will get transformed into a string in PHP land, so in PHP you have the strings '0' and '1'.

It is weird however that boolean casting does not convert them to the appropriate booleans. You may have some other bugs in your code.

Are you trying to cast the direct result from the query? Because that one is probably an array and:

var_dump((bool) array('0')); // true

Maybe this is your problem. Inspect the returned result.

Ionuț G. Stan
+1 For "other bugs in your code". A MySQL TINYINT column really only needs the `(bool)$value` treatment at most, in most contexts it'll work as is.
deceze
A: 

I use a helpful function "to_bool" for anything I'm not sure of the type of:

function to_bool($value, $default = false)
{
    if (is_bool($value)) {
        return $value;
    }
    if (!is_scalar($value)) {
        return $default;
    }
    $value = strtolower($value);
    if (strpos(";1;t;y;yes;on;enabled;true;", ";$value;") !== false) {
        return true;
    }
    if (strpos(";0;f;n;no;off;disabled;false;null;;", ";$value;") !== false) {
        return false;
    }
    return $default;
}

Then:

if (to_bool($row['result'])) { ... }
razzed
I don't know… There is a small, clear set of rules about how *any* PHP type converts to a boolean. `(bool)$value` is all you need. If you *expect* your values to be `"enabled"` or some such, you should check against that specifically. It's not something that should be generalized. Also, IMHO `in_array` would be a lot more readable than those `strpos` constructs.
deceze
You are probably right; I was thinking it may be faster with strpos, but I haven't tested. This is typically used to parse things from config files and external sources (type="checkbox" value="on"), and converting "true", 1, "on" etc. is pretty helpful a lot of the time. Unfotunately, (bool)"false" === true ... not terribly helpful.
razzed
Yes, strpos is faster: to_bool_strpos: 0.49599409103394, to_bool_in_array: 0.72869491577148; (100,000 iterations)
razzed