October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

PHP Array Operators Explained: Union (+), Equality (==), and Identity (===)

PHP’s array + operator is a left-biased union, not an overwrite merge. See how == and === compare arrays, why order and types matter, and which PHP version changes affect loose comparisons.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<?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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
$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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

A quick decision checklist

  1. Identify whether you are producing a new array or testing two arrays.
  2. For a new array, decide what should happen when both inputs have the same key.
  3. Choose + only when the left value must win on duplicates.
  4. For a test, decide whether order and exact types matter.
  5. Use == for pair-content equality when loose comparison is acceptable, or === for exact identity.
  6. 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.