php

Use of XOR operator in PHP

Different types of operators exist in PHP to perform logical operations. These are AND, OR, NOT, and XOR. These operators are used as a Boolean operator and bitwise operator. This tutorial mainly focuses on the Use of the XOR operator. The full form of XOR is exclusive, OR that works on two conditions. The XOR operator returns true when any condition returns true and returns false when both conditions return true or false. Xor keyword is used between the states to perform Boolean Xor operation, and ‘^’ symbol is used between the operands to perform bitwise xor operation. How the xor operator can be used for Boolean and bitwise operation, have shown in this tutorial.

xor Operator:

The Xor operator is used in the Boolean operation, and the bitwise operation is shown in the following table.

Condition-1 / Operand-1 Condition-1 / Operand-1 Output
True or 1 True or 1 False or 0
True or 1 False or 1 True or 1
False or 0 True or 1 True or 1
False or 0 False or 0 False or 0

Using XOR for Boolean operation:

The XOR operator’s different uses are explained in this section of this tutorial by using multiple examples.

Example -1: Using XOR in Boolean conditions of string data

The following example shows the Use of the xor operator to check the Boolean logic on string data. $customer_id and $client_id variables are initialized with the string data here. The first if condition with xor operator will check the first two characters of $customer_id is ‘AL’ or ‘CA’. Xor operator will return true for this if condition because one condition returns true. The second if condition with xor operator will check the first two characters of $customer_id is ‘AZ’ or ‘GA’. Xor operator will return false for this if condition because both conditions return false. The third if condition with xor operator will check the first two $customer_id is ‘CA’ or $client_id is ‘HI’. Xor operator will return false for this if condition because both conditions return true.

<?php

//Initialize the variables
$customer_id = 'CA-756345';
$client_id = 'HI-98765';

//XOR will return true if only one condition returns true
if(substr($customer_id,0,2) == 'AL' xor substr($customer_id,0,2) == 'CA')
{
//Find out which condition returned true
if(substr($customer_id,0,2) == 'AL')
echo "The customer($customer_id) lives in <b> Alabama </b><br />";
else
echo "The customer($customer_id) lives in <b> California </b><br />";
}
//XOR will return false if both conditions return false
if(substr($customer_id,0,2) == 'AZ' xor substr($customer_id,0,2) == 'GA')
echo "The customer($customer_id) lives in <b> Arizona </b> or <b> Georgia </b><br />";
else
echo "The customer($customer_id) niether lives in <b> Arizona </b> nor lives in <b> Georgia </b><br />";
//XOR will return false if both conditions return true
if(substr($customer_id,0,2) == 'CA' xor substr($client_id,0,2) == 'HI')
{
if(substr($customer_id,0,2) == 'CA')
echo "The customer($customer_id) lives in <b> California </b><br />";
else
echo "The customer($customer_id) lives in <b> Hawaii</b><br />";
}
else
{
//Find out the states of customer and client
if(substr($customer_id,0,2) == 'CA' and substr($client_id,0,2) == 'HI')
{
echo "The customer($customer_id) lives in <b> California </b><br />";
echo "The client($client_id) lives in <b> Hawaii </b><br />";
}
}
?>

Output:

The following output will appear after running the script.

Example-2: Using XOR in Boolean conditions of numeric data

The following example shows the Use of the xor operator to check the Boolean logic on numeric data. Two numeric values are assigned into $num1 and $num2 variables. The first if condition with xor operator will check $num1 is less than 3 or greater than 9. xor operator will return true for this if condition because $num1 is greater than 9. The second if condition with xor operator will check $num1 is less than or equal to 10 or $num2 is greater than or equal to 7. xor operator will return false for this if condition because both conditions are true. The third if condition with xor operator will check $num1 is greater than 19 or $num2 is equal to 17. xor operator will return false for this if condition because both conditions are false.

<?php

//Initialize the number values
$num1 = 10;
$num2 = 7;
//Retruns true if one condition is true
if($num1  9)
echo "The number is $num1.<br />";

//Returns true if cobditions are true
if($num1 = 7)
{
    if($num1 <= 10)
        echo "The condition is true for $num1.<br />";
    else
        echo "The condition is true for $num2.<br />";
    }
else
{
    if($num1 = 7)
        echo "Both conditions are true.<br />";
    else
        echo "Both conditions are false.<br />";
}

//Returns false if both conditions are false
if($num1 > 19 xor $num2 == 17)
{
    echo "One of the condition is true.<br />";
}
else
{
    if(!($num1  8))
        echo "Both conditions are false.<br />";
    else
        echo "Both conditions are true.<br />";
}

?>

Output:

The following output will appear after running the script.

Example-3: Using XOR in the bitwise operation

The following example shows the Use of the xor operator for bitwise operation. Two binary numbers are initialized into $number1 and $number2 variables. ‘^’ symbol is used to perform bitwise xor operation on binary numbers. The number value prints in decimal number by default for any script. decbin() function is used in the script to print the output in binary format.

<?php

//Two binary number is defined
$number1 = 0b111;
$number2 = 0b010;
//Use XOR for bitwise operation
$result = decbin($number1 ^ $number2);
//Print the result in binary
echo "The result of bitwise operation in binary is: $result";
?>

Output:

The following output will appear after running the script.

Conclusion:

The Xor operator’s uses between the Boolean conditions and the binary numbers have been explained using multiple examples in this tutorial. The readers will understand the xor operator’s logic and apply it to the conditional statement and bitwise operation after reading this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.