开发者

Boolean issues in PHP

开发者 https://www.devze.com 2022-12-28 01:19 出处:网络
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 fin

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 see开发者_如何转开发m 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.


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.


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

Try something like this:

$your_bool = $field_value === "0" ? false : true;


Using the script below. (You'll have to add HTML line break tags before the word "Boolean" inside the left quote to make the output look like my sample; when I do, Firefox interprets them, making the format look strange).

You'll see that the second line produces a null value which MySQL sees as something different from 0 or 1; for TINYINT it stores the PHP true value correctly but nothing for the PHP false, since a null value has no meaning for TINYINT.

Line four shows type casting with (int) is a way to insure that both PHP true and false are stored to MySQL TINYINT Boolean fields. Retrieving the resultant integers from MySQL into PHP variables works since integers are implicitly cast when assigned to PHP Boolean variables.

echo "Boolean true=".true;
echo "Boolean false=".false;
echo "Boolean (int)true=".(int)true;
echo "Boolean (int)false=".(int)false;

Here's the output from PHP 5.3.1 for MySQL 5.1.41:

Boolean true=1
Boolean false=
Boolean (int)true=1
Boolean (int)false=0

Oh! And PHP Boolean literals may be all lowercase or uppercase with the same result... try it yourself.


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'])) { ... }
0

精彩评论

暂无评论...
验证码 换一张
取 消