Exclusive or (xor)


#1

I’m expecting the following code testing xor to display ‘false’. But it displays true.

[php]

<?php // wasabi and mustard can't be applied at the same time. $wasabi='a'; $mustard='a'; $outcome= $wasabi xor $mustard; if ($outcome == false) { echo "false"; } if ($outcome == true) { echo "true"; } ?>

[/php]

THX


#2

Following works as expected

[php]

<?php // wasabi and mustard can't be applied at the same time. $wasabi='a'; $mustard='a'; if ($wasabi=='a' xor $mustard=='b') { echo 'true'; } ?>
<?php if ($wasabi=='b' xor $mustard=='a') { echo 'true'; } ?>
<?php if (!($wasabi=='a' xor $mustard=='a')) { echo 'false'; } ?>
<?php if (!($wasabi=='b' xor $mustard=='b')) { echo 'false'; } ?>

[/php]


#3

[php]

<?php // wasabi and mustard can't be applied at the same time. $wasabi=boolval("a")?'true':'false'; $mustard=boolval("a")?'true':'false'; $outcome= $wasabi xor $mustard; echo $wasabi;echo $mustard;echo $outcome; // truetruetrue /* if ($outcome == 'false') { echo "false"; } if ($outcome == 'true') { echo "true"; } */ ?>

[/php]

Here $outcome is true even when $wasabi and $mustard are both true. ???


#4

I fixed the error. It was with lower precedence of xor compared with =.

[php]

<?php $outcome=(true xor true); if ($outcome==true) echo "true"; elseif ($outcome==false) echo "false"; ?>
<?php $outcome2=(false xor false); if ($outcome2==true) echo "true"; elseif ($outcome2==false) echo "false"; ?>
<?php $outcome3=(true xor false); if ($outcome3==true) echo "true"; elseif ($outcome3==false) echo "false"; ?>
<?php $outcome4=(false xor true); if ($outcome4==true) echo "true"; elseif ($outcome4==false) echo "false"; ?>

[/php]

This works as expected
false
false
true
true


#5

Just to wrap it up the following works as expected

[php]

<?php $outcome=('a' xor 'a'); if ($outcome==true) echo "true"; elseif ($outcome==false) echo "false"; ?>
<?php $outcome2=('' xor ''); if ($outcome2==true) echo "true"; elseif ($outcome2==false) echo "false"; ?>
<?php $outcome3=('a' xor ''); if ($outcome3==true) echo "true"; elseif ($outcome3==false) echo "false"; ?>
<?php $outcome4=('' xor 'a'); if ($outcome4==true) echo "true"; elseif ($outcome4==false) echo "false"; ?>

[/php]

I get

false
false
true
true