Home > Articles > Programming > PHP

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Using the Array Operators

Want more array power? Check out the array operators:

$a + $b

Union of $a and $b.

$a == $b

TRUE if $a and $b have the same elements.

$a === $b

TRUE if $a and $b have the same elements in the same order.

$a != $b

TRUE if $a is not equal to $b.

$a <> $b

TRUE if $a is not equal to $b.

$a !== $b

TRUE if $a is not identical to $b.

Most of these have to do with comparing arrays, but the + operator is designed to concatenate arrays. You can see an example in phparrayops.php, Example 3-6, where we put to work not only the + operator but also the == operator, checking to see if two arrays have the same elements (in this case, they don't).

Example 3-6. Using array operators, phparrayops.php

                Using array operators

                Using array operators
                $fruits["apples"] = 3839;
                $fruits["oranges"] = 2289;
                $vegetables["broccoli"] = 1991;
                $vegetables["corn"] = 9195;
                echo "\$fruits: ";
                echo "<BR>";
                echo "\$vegetables: ";
                echo "<BR>";
                $produce = $fruits + $vegetables;
                echo "\$produce: ";
                echo "<BR>";
                if ($fruits == $vegetables){
   echo "\$fruits has the same elements as \$vegetables<BR>";
   else {
   echo "\$fruits does not have the same elements as

The results appear in Figure 3-6, where you can see that the + operator did indeed concatenate the arrays we wanted it to, and the == operator did indeed compare the two arrays properly.


Figure 3-6 Using array operators.

  • + Share This
  • 🔖 Save To Your Account