1

ユーザーを更新するためにプログラムにセクションを書いていますが、パスワードとパスワードの確認があります。フィールドに入力していない限り、DBのパスワードフィールドには何もしたくありません。

したがって、次のエラーチェックがあります。

if(($sql['password']!="") && ($sql['cpassword']!=""))
            {
                if($sql['password'] == $sql['cpassword'])
                {
//update
}

}

ただし、パスワード フィールドの 1 つだけを入力した場合にエラー メッセージをスローする簡単な行を書きたいと思います。だから私は考え出した:

if($sql['password'] ^ $sql['cpassword']) 
            {
                echo You must fill out <i>both</i> password fields and they <i>must</i> match.";
            }

やりますが、そうではないようです。次に、NOT 演算子 ! を追加しましたが、うまくいくように見えましたが、両方のフィールドに何もない場合でも、エラー メッセージが表示されます :(

この回答からわかるロジックから、単純な XOR が機能するように思われます。しかし、そうではありません。誰かが私に理由を説明できますか?

4

1 に答える 1

3

XOR は確かにここで必要なものではありません。

Davidが述べているように、値が異なる場合、XORは「真」の結果を返しますが、2つの文字列をXORすると、XOR操作は最短の文字列までしか行われません。

Examples

'AAAA' ^ 'AAAA' This returns an empty string (false-equivalent value) as the values are the same.

'AAAA' ^ 'bbbb' This returns an non-empty string (true-equivalent value)..

'AAAA' ^ 'AAAAbbbb' This returns an empty string (false-equivalent value), even though the strings are different. This is because the result of the operation only considers the first 4 characters.

In Sandy Lee's example (bool)$string does not really help.

(bool)'0' = false

(bool)'1' = true

This does not tell you if the string is empty or not. It simply gives you the boolean-equivalent value of the string.

There is no need to use XOR here at all. It's not the right tool for the job. There is no need to try and do anything fancy either, the simple tools work perfectly.

$password = trim($password);
$confirm = trim($confirm);

if (!$password || !$confirm) {
    // One of the fields was not completed.
}
elseif ($password !== $confirm) {
    // Fields did not match
}
else {
    // Update password
}
于 2013-01-28T13:32:32.533 に答える