What is the Ds\Set xor() function in PHP?

The Ds\Set::xor() function in PHP returns the XOR (exclusive-OR) of two sets.

The XOR of two sets A and B, contains all the elements that are either in set A or set B, but not in both. It can be calculated as: A XOR B = (A Union B) - (A Intersection B)

For example, consider two sets A and B such that:

A = {1, 2, 3, 4}

B = {3, 4, 5, 6}

The XOR of A and B can be calculated as:

A Union B = {1, 2, 3, 4, 5, 6}

A Intersection B = {3, 4}

A XOR B = {1, 2, 3, 4, 5, 6} - {3, 4}

A XOR B = {1, 2, 5, 6}

Syntax

The xor() function can be declared as shown in the code snippet below:

Ds\Set public Ds\Set::xor ( Ds\Set $set )
  • set: This is the set with which to calculate the XOR.

Return value

The xor() function returns a set that contains the resulting set after computing the XOR of two sets.

Example

The code snippet below demonstrates the use of the xor() function.

<?php
$s1 = new \Ds\Set([1, 2, 3, 4]);
$s2 = new \Ds\Set([3, 4, 5, 6]);
echo("xor(s1, s2): \n");
print_r($s1->xor($s2));
?>

Explanation

  • Line 3: We declare a set named s1.
  • Line 5: We declare a set named s2.
  • Line 9: The xor() function returns the XOR of s1 and s2.

Free Resources