Google News
logo
PHP Program to Array Operators
In the following example of PHP program that demonstrates the use of array operators :
Program :
<?php

// Declare two arrays
$arr1 = array(1, 2, 3);
$arr2 = array(2, 3, 4);

// Union operator
$union = $arr1 + $arr2;
echo "Union operator: ";
print_r($union);

// Equality operator
if ($arr1 == $arr2) {
    echo "Arrays are equal <br>";
} else {
    echo "Arrays are not equal <br>";
}

// Identity operator
if ($arr1 === $arr2) {
    echo "Arrays are identical <br>";
} else {
    echo "Arrays are not identical <br>";
}

// Inequality operator
if ($arr1 != $arr2) {
    echo "Arrays are not equal <br>";
} else {
    echo "Arrays are equal <br>";
}

// Non-identity operator
if ($arr1 !== $arr2) {
    echo "Arrays are not identical <br>";
} else {
    echo "Arrays are identical <br>";
}

// Difference operator
$diff = array_diff($arr1, $arr2);
echo "Difference operator: ";
print_r($diff);

// Intersection operator
$intersect = array_intersect($arr1, $arr2);
echo "Intersection operator: ";
print_r($intersect);

?>
Output :
Union operator: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
Arrays are not equal
Arrays are not identical
Arrays are not equal
Arrays are not identical
Difference operator: Array ( [0] => 1 )
Intersection operator: Array ( [1] => 2 [2] => 3 )
In this example, we have declared two arrays `$arr1` and `$arr2` and assigned them the values `(1, 2, 3)` and `(2, 3, 4)`, respectively.

We then use array operators to combine, compare, and perform set operations on the arrays. Finally, we print out the results using the `print_r` statement.