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

<HTML>
        <HEAD>
            <TITLE>
                Using array operators
            </TITLE>
        </HEAD>

        <BODY>
            <H1>
                Using array operators
            </H1>
            <?php
                $fruits["apples"] = 3839;
                $fruits["oranges"] = 2289;
                $vegetables["broccoli"] = 1991;
                $vegetables["corn"] = 9195;
                echo "\$fruits: ";
                print_r($fruits);
                echo "<BR>";
                echo "\$vegetables: ";
                print_r($vegetables);
                echo "<BR>";
                $produce = $fruits + $vegetables;
                echo "\$produce: ";
                print_r($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
                        
   \$vegetables<BR>";
                
   }
            ?>
    </BODY>
</HTML>

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.

03fig06.gif

Figure 3-6 Using array operators.

  • + Share This
  • 🔖 Save To Your Account