tags:

views:

128

answers:

5

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?

+5  A: 

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

Chacha102
See my edit above. I believe that's causing the problem for me...
Scott B
-1: `true` is not a constant set to `1`. `true` is a language construct. The reason why `true == 1` is because the only integer considered to be false is `0`. Therefore, `true == -1`, `true == 3`, `true == 978213`, `true != 0`, etc. Please see http://php.net/boolean
Andrew Moore
And actually, when I run `get_defined_constants`, I get `["TRUE"]=>bool(true),["FALSE"]=>bool(false)` on 5.2.12...
Andrew Moore
@Chacha102: Read my above comment, constant `TRUE` = `bool(true)` and constant `FALSE` = `bool(false)`... `true` and `false`... Please run `get_defined_constants` yourself... To a `var_dump` on its return value.
Andrew Moore
I'm deleting all my comments. I removed it. Enjoy.
Chacha102
Please don't delete your comments in a case like this, even if you're angry or wrong. Now the comment thread cannot be used by others to learn something later.
Cory Petosky
A: 

"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.

chelmertz
Not true; strings equate to "True" when cast to bool, except the string "0", and the empty string "", which are false.
meagar
@meagar, thanks was too fast (edited.)
chelmertz
+1  A: 

Try

if ($myvar)

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

Cory Petosky
This is terrible practice. In that case PHP would try to automatically casts the variable and it would return true even if it wasn't. Your snippet of code will return true for anything other than 0, false or null. That might work in this case but is just wrong to do.
Alex
It might be terrible practice where wordpress is concerned, but in a well written application, where you treat flags or options as booleans, this would be the way to compare it. Unfortunately wordpress won't convert them to booleans upon retrieval from the database, and you're left facing a weakly typed mess.
David Caunt
@Alex, you're correct, which is precisely why this is the correct form. If you don't know if a variable is meant to be boolean before you toss it in the if statement, 98% of the time you're doing it wrong (and the other 2% you likely care only if the variable is set at all, which means that bare `if` is correct anyway).
Cory Petosky
A: 

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".

meagar
A: 

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.

Gordon