Question

EDIT: I believe my confusion is probably created by this code at the top of the page in which I'm testing for the value of the option... This creates a shortcut method to refer to the option without using the get_option('option') method...

global $options;
foreach ($options as $value) {
if (get_settings( $value['id'] ) === FALSE) { 
        $$value['id'] = $value['std']; 
    } else { 
        $$value['id'] = get_settings( $value['id'] );
    }
}

And so when I set the value of a variable, $myvar, via a checkbox checked in my theme's options panel and click save, then view my options.php in worpdress, the value of the variable is

true

And when I do a lookup on this variable using

if($myvar == "true")

It passes.

However, when I set the value directly via the update_options() method, like so...

$mvar = true;
update_option('myvar', $myvar);

The value changes from true to 1

And when I do the same comparison as before, if($myvar == "true"), it now fails. It is no longer "true".

What am I missing? (1) why is "true" and 1, not evaluating the same and (2) What is the update_option method doing to the value of myvar to change the value from true to 1?

Was it helpful?

Solution

Try

if($myvar == true)

and

$myvar = true;

TRUE and FALSE are PHP's built in boolean variables which are much more universal than a true string.


About the update_option. It might not be that the option is changing it to 1. Instead it might be that the when it is inserting it into the database, it inserts it as the string "true". Then, when it comes back it is converted to the boolean value true, which when printed is 1

OTHER TIPS

Try

if ($myvar)

Don't test whether things "equal" true, they are either true or they aren't.

You should change your first test to if($myvar == true) or simply if ($myvar). PHP has some strange rules for what is "true"; Generally, strings evaulate to true, except the special cases of "0" and an empty string "", which type-cast to false.

In your specific example, if ($myvar == "true"):

  • If $myvar contains a boolean, the expression will evaluate as (bool) == (bool)"true", or (bool) == true
  • If $myvar contains an integer, it's cast to a string and compared against the string "true"; so your test is failing, because "1" != "true".
  • If $myvar is a string, string comparison takes place and suddenly only the literal string "true" will successfully compare.

I would guess 2nd and 3rd cases are in effect: Wordpress is likely setting $myval to the string "true" when the from is posted back, so your test passes. When you manually specify boolean true, Wordpress must be converting it to an integer, and then integer comparison takes place and fails, because the integer 1 will be cast to string "1" when compared against "true".

You are doing a loose comparison between the integer 1 and the string 'true'. For this PHP will translate the string to a number. 'test' as a number is 0:

var_dump((int) 'true'); // int(0)

And since 0 is not equal to 1, the comparison will return false.

Like some other answers already correctly pointed out, you should test against the boolean literal TRUE or true. If one operator in a equality check is a boolean, PHP will convert the other operator to a boolean too, which, for the number 1, will give

var_dump((bool) 1); // boolean(true)

And then your test will pass, because true is equal to true.

Check out the Type Comparison Table to understand how PHP juggles types when testing for equality.

As for what check_update does to your boolean, check out the function description:

(mixed) (required) The NEW value for this option name. This value can be a string, an array, an object or a serialized value.

So, no boolean allowed. I've tried briefly to find out from the sourcecode where the conversion takes place, but since WP is a mess to me, couldn't find it. Like someone else suggested, it probably happens when storing it to the database and then getting it back.

"true" is a string, and all strings evaulates to the boolean 1 (try casting (bool) $string. true on the other hand, without quotes, is a boolean and will evaluate to 1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow