Q:

Understanding XOR PHP

I am writing section in my program to update a user, and there is password and confirm passwords.  I dont want to do anything to the password field in the DB unless they have filled the fields in.
So I have error checking for:
if(($sql['password']!="") && ($sql['cpassword']!=""))
            {
                if($sql['password'] == $sql['cpassword'])
                {
//update
}

}
However I want to write a quick line to throw an error message if they only filled out one of the password fields.  So I figured:
if($sql['password'] ^ $sql['cpassword']) 
            {
                echo You must fill out <i>both</i> password fields and they <i>must</i> match.";
            }

Would do it, but It doesnt seem to.  Then I added a NOT operator, !, and that seemed to work well, however if both fields have nothing in them, I still get the error message :(
From the logic I can see from this answer, It would seem that simple XOR would work.  But
But it doesnt, Can someone explain to me why?

A:

XOR certainly is not what you want here.
As David states, XOR will return a "true" result if the values are different, however when you XOR two strings the XOR operation is only done up to the point of the shortest string.
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
}

