문제

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?

도움이 되었습니까?

해결책

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

다른 팁

자격 증명을 다음과 같이 전달할 수 있습니다.

ClientContext clientContext = new ClientContext(siteUrl);
clientContext.Credentials = new NetworkCredential("Login id", "Password", "Domain");
.

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

나는 당신이 강제로 닫히고 있다고 확신합니다 :

여기에 이미지 설명

안드로이드 에뮬레이터는 후크에 오디오 입력이 없기 때문에 대신 마이크 또는 헤드폰을 연결하고 테스트하십시오.

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top