Home > Articles > Programming > PHP

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

Array Operators

One set of special operators applies only to arrays. Most of them have an analogue in the scalar operators, as you can see by looking at Table 3.1.

Table 3.1 PHP's Array Operators







$a + $b

Union of $a and $b. The array $b is appended to $a, but any key clashes are not added.



$a == $b

True if $a and $b contain the same elements.



$a === $b

True if $a and $b contain the same elements in the same order.



$a != $b

True if $a and $b do not contain the same elements.



$a <> $b

Same as !=.



$a !== $b

True if $a and $b do not contain the same elements in the same order.

These operators are mostly fairly self-evident, but union requires some further explanation. The union operator tries to add the elements of $b to the end of $a. If elements in $b have the same keys as some elements already in $a, they will not be added. That is, no elements of $a will be overwritten.

You will notice that the array operators in Table 3.1 all have equivalent operators that work on scalar variables. As long as you remember that + performs addition on scalar types and union on arrays—even if you have no interest in the set arithmetic behind that behavior—the behaviors should make sense. You cannot usefully compare arrays to scalar types.

  • + Share This
  • 🔖 Save To Your Account