PHP - Ds Set::xor() Function
The PHP Ds\Set::xor() function is used to create a new set by using the values in the current instance and another set but not in both.
Which means that, if the values are equal in both sets, then the set returned by this function will be empty ([]). For example, set1 = ([1, 2, 3]), and set2 = ([1, 2, 3]), the set1->xor(set2) = ([]).
Following is the formula to calculate the XOR of two sets −
A XOR B = (A Union B) - (A Intersection B)
Here, A is considered as the first set, and B is considered as the second set.
Syntax
Following is the syntax of the PHP Ds\Set::xor() function −
public Ds\Set Ds\Set::xor( Ds\Set $set )
Parameters
Following is the parameter of this function −
- set − The other set holds the set of values.
Return value
This function returns the XOR of two sets, exclusive OR.
Example 1
The following is the basic example of the PHP Ds\Set::xor() function −
<?php
$set1 = new \Ds\Set([1, 2, 3,]);
$set2 = new \Ds\Set([2, 3, 4, 5]);
echo "The set1 elements are: \n";
print_r($set1);
echo "The set2 elements are: \n";
print_r($set2);
echo("The xor of both set: \n");
#using xor() method
var_dump($set1->xor($set2));
?>
Output
The above program produces the following output −
The set1 elements are:
Ds\Set Object
(
[0] => 1
[1] => 2
[2] => 3
)
The set2 elements are:
Ds\Set Object
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
)
The xor of both set:
object(Ds\Set)#3 (3) {
[0]=>
int(1)
[1]=>
int(4)
[2]=>
int(5)
}
Example 2
Following is another example of the PHP Ds\Set::xor() function. We use this method to retrieve a new set containing values in this set (["Hello", "Tutorials", "Point", "India"]) and another set (["Tutorials", "Point", "India"]), but not in both.
<?php
$set1 = new \Ds\Set(["Hello", "Tutorials", "Point", "India"]);
$set2 = new \Ds\Set(["Tutorials", "Point", "India"]);
echo "The set1 elements are: \n";
print_r($set1);
echo "The set2 elements are: \n";
print_r($set2);
echo("The xor of both set: \n");
#using xor() method
var_dump($set1->xor($set2));
?7gt;
Output
After executing the above program, it will display the following output −
The set1 elements are:
Ds\Set Object
(
[0] => Hello
[1] => Tutorials
[2] => Point
[3] => India
)
The set2 elements are:
Ds\Set Object
(
[0] => Tutorials
[1] => Point
[2] => India
)
The xor of both set:
object(Ds\Set)#3 (1) {
[0]=>
string(5) "Hello"
}
Example 3
If the values in both sets are exactly similar, the XOR() function returns an XOR of both sets as a new empty set ([]).
<?php
$set1 = new \Ds\Set(['a', 'e', 'i', 'o', 'u']);
$set2 = new \Ds\Set(['a', 'e', 'i', 'o', 'u']);
echo "The set1 elements are: \n";
print_r($set1);
echo "The set2 elements are: \n";
print_r($set2);
echo("The xor of both set: \n");
#using xor() method
var_dump($set1->xor($set2));
?>
Output
Once the above program is executed, it will generate the following output −
The set1 elements are:
Ds\Set Object
(
[0] => a
[1] => e
[2] => i
[3] => o
[4] => u
)
The set2 elements are:
Ds\Set Object
(
[0] => a
[1] => e
[2] => i
[3] => o
[4] => u
)
The xor of both set:
object(Ds\Set)#3 (0) {
}