McNabbToSkins
McNabbToSkins

Reputation: 257

Boolean issues in PHP

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.

Upvotes: 3

Views: 7131

Answers (4)

Taylor Hanna
Taylor Hanna

Reputation: 11

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.

Upvotes: 1

Ionuț G. Stan
Ionuț G. Stan

Reputation: 179119

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.

Upvotes: 4

razzed
razzed

Reputation: 2683

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

Upvotes: 0

Andrew Hare
Andrew Hare

Reputation: 351516

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

Try something like this:

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

Upvotes: 1

Related Questions