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.

Figure 3-6. Using array operators.




    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net