php

Use of XOR operator in 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.

//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 Alabama
";
else
echo "The customer($customer_id) lives in California
";

//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 Arizona or Georgia
";
else
echo "The customer($customer_id) niether lives in Arizona nor lives in Georgia
";
//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 California
";
else
echo "The customer($customer_id) lives in Hawaii
";

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 California
";
echo "The client($client_id) lives in Hawaii
";


?>

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.

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

if($num1 <= 10)
echo "The condition is true for $num1.
";
else
echo "The condition is true for $num2.
";

else

if($num1 = 7)
echo "Both conditions are true.
";
else
echo "Both conditions are false.
";

//Returns false if both conditions are false
if($num1 > 19 xor $num2 == 17)

echo "One of the condition is true.
";

else

if(!($num1 8))
echo "Both conditions are false.
";
else
echo "Both conditions are true.
";

?>

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.

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

Gry Jak korzystać z GameConqueror Cheat Engine w systemie Linux
Jak korzystać z GameConqueror Cheat Engine w systemie Linux
Artykuł zawiera przewodnik dotyczący korzystania z silnika oszukującego GameConqueror w systemie Linux. Wielu użytkowników, którzy grają w gry w syste...
Gry Najlepsze emulatory konsoli do gier dla systemu Linux
Najlepsze emulatory konsoli do gier dla systemu Linux
W tym artykule wymienimy popularne oprogramowanie do emulacji konsoli do gier dostępne dla systemu Linux. Emulacja to warstwa kompatybilności oprogram...
Gry Najlepsze dystrybucje Linuksa do gier w 2021 r
Najlepsze dystrybucje Linuksa do gier w 2021 r
System operacyjny Linux przeszedł długą drogę od pierwotnego, prostego, serwerowego wyglądu. Ten system operacyjny znacznie się poprawił w ostatnich l...