In PHP, $a + $b performs a left-biased array union: keys already present in $a keep their values, and only missing keys are taken from $b. It does not overwrite duplicates. For comparisons, == checks key/value pairs without requiring their order to match, while === also requires identical order and types.
What PHP’s array operators mean
The PHP manual groups the principal array operators as follows:
| Operator | Meaning |
|---|---|
$a + $b |
Union |
$a == $b |
Equality |
$a === $b |
Identity |
$a != $b or $a <> $b |
Inequality |
$a !== $b |
Non-identity |
These operators are distinct from relational operators such as <, >, <=, >=, and <=>. See the PHP array-operator manual for the language definitions.
How array union with + handles duplicate keys
The left-hand array wins
Union combines key/value pairs while preserving the left operand whenever a key exists on both sides:
#1 Best Overall
<?php
$a = ['a' => 'apple', 'b' => 'banana'];
$b = ['a' => 'pear', 'b' => 'strawberry', 'c' => 'cherry'];
$result = $a + $b;
print_r($result);
The result is:
Array
(
[a] => apple
[b] => banana
[c] => cherry
)
The right-hand values for a and b are ignored because those keys already exist in $a. The new c key is appended.
Reversing operands changes the result
$result = $b + $a;
Now a is pear and b is strawberry, because $b is the left operand. Union is therefore intentionally left-biased and is not an overwrite merge.
Rank #2
Compound union assignment
$a += $b;
This applies the same rule in place: existing keys in $a retain their values, while keys absent from $a are copied from $b. Use it when preserving the original array’s values is the desired policy.
Practical implications for keys
- Duplicate keys are resolved by the left side.
- Keys are used for matching; values alone do not create a duplicate.
- Union is useful for defaults, where a user or application array should take precedence over fallback values.
- If you need replacement or a different key-handling policy, use the appropriate documented array function rather than assuming
+performs a merge. The PHP array-function reference lists those functions.
== versus === for arrays
==: same key/value pairs, order can differ
Loose array equality succeeds when both arrays contain the same key/value pairs, even if the pairs appear in a different order:
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
$one = ['apple', 'banana'];
$two = [1 => 'banana', '0' => 'apple'];
var_dump($one == $two); // bool(true)
PHP treats the string key '0' as the integer key 0 here, so the sets of key/value pairs match. Their insertion order is different, but == does not require that order to match.
===: same pairs, same order, same types
var_dump($one === $two); // bool(false)
Strict identity requires the same key/value pairs in the same order and with the same types. In the example, the ordering differs, so identity fails even though loose equality succeeds. This is the safer choice when an array’s exact representation matters, such as a canonical configuration or a serialized structure.
Rank #4
Choosing the comparison
| Question | Use | What must match |
|---|---|---|
| Do these arrays contain the same pairs, regardless of order? | == |
Keys and values, subject to PHP’s loose comparison rules |
| Are these arrays exactly the same representation? | === |
Keys, values, order, and types |
| Must they differ in exact identity? | !== |
The opposite of === |
Loose-comparison and PHP-version caveats
Type juggling can affect ==
Loose comparisons may convert values before comparing them. The comparison-operator manual documents these rules and recommends strict operators when automatic conversion could be surprising.
Version matters for number/string comparisons. Before PHP 8.0.0, a string compared with a number (or numeric string) was converted to a number first. PHP 8.0.0 changed this behavior, so an expression such as 0 == 'a' can produce a different result between PHP 7 and PHP 8. When diagnosing a comparison bug, record the PHP version and the operand types instead of relying on a result observed on another runtime.
Recommended Free Tools
Recursive arrays and PHP 8.4.0
Comparisons involving recursive structures have a separate version-sensitive edge case. PHP 8.4.0 and later throw an Error when recursion is encountered during comparison; earlier versions produced a fatal error according to the manual. Avoid comparing self-referential arrays without handling this possibility.
Array comparison is not the same as combining arrays
+ creates a union result. ==, ===, !=, and !== answer questions about two existing arrays; they do not merge them. Keep the operation’s intent explicit:
- Need fallback values without replacing supplied values? Use left-biased union.
- Need to test equivalent key/value content while ignoring order? Use
==, accepting its loose comparison rules. - Need exact order and type matching? Use
===. - Need a different merge, replacement, filtering, or difference policy? Select a function from the array-function reference whose documented behavior matches that policy.
Related array features
PHP also supports comparing arrays with functions such as those documented in the array reference and supports array unpacking with ... as of PHP 7.4.0. Unpacking is a separate syntax feature, not another entry in the array-operator table; check the arrays manual for its rules and limitations.
Quick Recap
A quick decision checklist
- Identify whether you are producing a new array or testing two arrays.
- For a new array, decide what should happen when both inputs have the same key.
- Choose
+only when the left value must win on duplicates. - For a test, decide whether order and exact types matter.
- Use
==for pair-content equality when loose comparison is acceptable, or===for exact identity. - If numbers and strings are mixed, verify behavior on the PHP version that runs your code.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




