Quoting from the PHP Manual on Language Operators

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

So if you do

$array1 = ['one',   'two',          'foo' => 'bar'];
$array2 = ['three', 'four', 'five', 'foo' => 'baz']; 

print_r($array1 + $array2);

You will get

Array
(
    [0] => one   // preserved from $array1 (left-hand array)
    [1] => two   // preserved from $array1 (left-hand array)
    [foo] => bar // preserved from $array1 (left-hand array)
    [2] => five  // added from $array2 (right-hand array)
)

So the logic of + is equivalent to the following snippet:

$union = $array1;

foreach ($array2 as value) {
    if (false === array_key_exists(union)) {
        $union[value;
    }
}

If you are interested in the details of the C-level implementation head to

  • php-src/Zend/zend_operators.c

Note, that + is different from how array_merge() would combine the arrays:

print_r(array_merge($array1, $array2));

would give you

Array
(
    [0] => one   // preserved from $array1
    [1] => two   // preserved from $array1
    [foo] => baz // overwritten from $array2
    [2] => three // appended from $array2
    [3] => four  // appended from $array2
    [4] => five  // appended from $array2
)

See linked pages for more examples.

Answer from Gordon on Stack Overflow
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ language.operators.array.php
PHP: Array - Manual
For instance: <?php $a = array('one','two'); $b=array('three','four','five'); //not a union of arrays' values echo '$a + $b : '; print_r ($a + $b); //a union of arrays' values echo "array_unique(array_merge($a,$b)):"; // cribbed from http://oreilly.com/catalog/progphp/chapter/ch05.html print_r (array_unique(array_merge($a,$b))); ?> //output $a + $b : Array ( [0] => one [1] => two [2] => five ) array_unique(array_merge(Array,Array)):Array ( [0] => one [1] => two [2] => three [3] => four [4] => five ) ... The example may get u into thinking that the identical operator returns true because the key of apple is a string but that is not the case, cause if a string array key is the standart representation of a integer it's gets a numeral key automaticly.
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_operators.asp
PHP Operators
The string operators are used to concatenate strings. The array operators are used to compare arrays.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ home โ€บ php โ€บ php array operators
PHP Array Operators
May 26, 2007 - These operators are useful for a variety of tasks, like merging two arrays, testing if two arrays are equal and finding whether they have the same order and data types. PHP defines the following set of symbols to be used as operators on array data types โˆ’
๐ŸŒ
w3resource
w3resource.com โ€บ php โ€บ operators โ€บ array-operators.php
PHP array Operators - w3resource
In the following example equality operator returns true as the two arrays have same key/value pairs whereas identity operator returns false as the key/value of the comparing arrays are same but not in same order. <?php $a = array("1" => "apple", "0" => "banana"); $b = array( "banana", "apple"); var_dump($a == $b); var_dump($a === $b); ?>
Top answer
1 of 9
323

Quoting from the PHP Manual on Language Operators

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

So if you do

$array1 = ['one',   'two',          'foo' => 'bar'];
$array2 = ['three', 'four', 'five', 'foo' => 'baz']; 

print_r($array1 + $array2);

You will get

Array
(
    [0] => one   // preserved from $array1 (left-hand array)
    [1] => two   // preserved from $array1 (left-hand array)
    [foo] => bar // preserved from $array1 (left-hand array)
    [2] => five  // added from $array2 (right-hand array)
)

So the logic of + is equivalent to the following snippet:

$union = $array1;

foreach ($array2 as value) {
    if (false === array_key_exists(union)) {
        $union[value;
    }
}

If you are interested in the details of the C-level implementation head to

  • php-src/Zend/zend_operators.c

Note, that + is different from how array_merge() would combine the arrays:

print_r(array_merge($array1, $array2));

would give you

Array
(
    [0] => one   // preserved from $array1
    [1] => two   // preserved from $array1
    [foo] => baz // overwritten from $array2
    [2] => three // appended from $array2
    [3] => four  // appended from $array2
    [4] => five  // appended from $array2
)

See linked pages for more examples.

2 of 9
26

The best example I found for using this is in a config array.

$user_vars = array("username"=>"John Doe");
$default_vars = array("username"=>"Unknown", "email"=>"[email protected]");

$config = $user_vars + $default_vars;

The $default_vars, as it suggests, is the array for default values. The $user_vars array will overwrite the values defined in $default_vars. Any missing values in $user_vars are now the defaults vars from $default_vars.

This would print_r as:

Array(2){
    "username" => "John Doe",
    "email" => "[email protected]"
}

I hope this helps!

๐ŸŒ
SitePoint
sitepoint.com โ€บ blog โ€บ php โ€บ array operators in php: interesting but less spoken
PHP Master | Array Operators in PHP: Interesting but Less Spoken
November 7, 2024 - This article details working with array operators, but also covers how some of the other operators work when used with arrays. PHP array operators are categorized into Union, Equality, Identity, Inequality, and Non-Identity, each performing unique functions like merging arrays, checking if ...
๐ŸŒ
FlatCoding
flatcoding.com โ€บ home โ€บ php array operators: union, equality, identity
PHP Array Operators: Union, Equality, Identity - FlatCoding
April 15, 2025 - Throughout this exploration, we delved into various array operators, including the Union Operator (+), Equality Operator (==), Identity Operator (===), Inequality Operator (!=), and the Spaceship Operator (<=>). These operators equip developers with the means to perform diverse operations on ...
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ php-operators
PHP Operators - GeeksforGeeks
June 14, 2025 - Here are the array operators, along with their syntax and operations, that PHP provides for the array operation.
๐ŸŒ
Dino Cajic
dinocajic.com โ€บ home โ€บ programming โ€บ php โ€” p21: array operators
PHP Array Operators - Basic PHP Operators - Dino Cajic
July 20, 2022 - PHP Array operators are operators that are used on arrays. Who would have guessed? The first operator that weโ€™ll look at is the + operator.
๐ŸŒ
InfoQ
infoq.com โ€บ articles โ€บ php-7-array-operators
PHP 7 โ€” Improvements to Arrays, Operators, Constants, and Exception Handling - InfoQ
August 10, 2020 - PHP 7.0 adds a new comparison operator (<=>) to compare expressions. PHP 7.0 adds support for Unicode codepoint escape syntax, to convert an hexadecimal form to the corresponding UTF-8 encoded form. The use statement may group classes, functions, and constants even when imported from the same namespace. PHP 7.1, adds a short form array syntax for unpacking or destructuring an array.
๐ŸŒ
BCCNsoft
doc.bccnsoft.com โ€บ docs โ€บ php-docs-7-en โ€บ language.operators.array.html
Array Operators
Union of $a and $b: array(3) { ["a"]=> string(5) "apple" ["b"]=> string(6) "banana" ["c"]=> string(6) "cherry" } Union of $b and $a: array(3) { ["a"]=> string(4) "pear" ["b"]=> string(10) "strawberry" ["c"]=> string(6) "cherry" } Union of $a += $b: array(3) { 'a' => string(5) "apple" 'b' => string(6) "banana" 'c' => string(6) "cherry" }
๐ŸŒ
InfoQ
infoq.com โ€บ articles โ€บ php8-arrays-variables-operators
PHP 8 - Arrays, Variables, Operators, Exception Handling and More - InfoQ
February 13, 2023 - PHP 8 supports array unpacking with string keys, introduces a new function to determine if an array is a list, and a stable array sort function. Exception handling adds support for non-capturing catches, and use of the throw statement where only an expression is usable. New features related to variable declarations are explicit octal notation 0o/0O for integer literals, limited $GLOBALS usage, and namespaced names as single tokens. New operators include an improved non-strict string to number comparison, the new null-safe operator, locale-independent float to string cast, and stricter type checks for arithmetic/bitwise operators.
๐ŸŒ
PHP
wiki.php.net โ€บ rfc โ€บ spread_operator_for_array
PHP: rfc:spread_operator_for_array
Only arrays and objects who implement Traversable can be expanded. ... $parts = ['apple', 'pear']; $fruits = ['banana', 'orange', ...$parts, 'watermelon']; // ['banana', 'orange', 'apple', 'pear', 'watermelon']; It's possible to do the expansion multiple times, and unlike argument unpacking, ... can be used anywhere. It's possible to add normal elements before or after the spread operator.
Top answer
1 of 4
24

$arr1 += $arr2 is short for $arr1 = $arr1 + $arr2.

The + array operator does the following:

  • Create a new array that contains all the elements of $arr1 and $arr2, except for the next condition.
  • If both operands have elements with the same key, only the element of $arr1 will be present.
  • The elements of $arr2 will be after those of $arr1.

This is different from array_merge, which:

  • Creates a new array that contains all the elements of $arr1 and $arr2, except for the next condition.
  • If both operands have elements with the same string key, only the element of $arr2 will be present.
  • Elements with numeric keys will be renumbered from 0, starting with the elements of $arr1, and then moving to the elements of $arr2.
  • The elements of $arr2 will be after those of $arr1, except the string elements, which will be in the position of the first array in which they appear.

Example:

<?php
$arr1 = array(1 => 'value1.1', 10 => 'value1.2', 's' => 'value1.s');
$arr2 = array(1 => 'value2', 2=> 'value2.2', 's' => 'value2.s');
var_dump(array_merge($arr1,$arr2));
$arr1 += $arr2;
var_dump($arr1);

Result (edited for clarity):

array(5) {
  [0]   => string(8) "value1.1"
  [1]   => string(8) "value1.2"
  ["s"] => string(8) "value2.s"
  [2]   => string(6) "value2"
  [3]   => string(8) "value2.2"
}
array(4) {
  [1]   => string(8) "value1.1"
  [10]  => string(8) "value1.2"
  ["s"] => string(8) "value1.s"
  [2]   => string(8) "value2.2"
}
2 of 4
4

The + operator in PHP when applied to arrays does the job of array UNION.

$arr += array $arr1;

effectively finds the union of $arr and $arr1 and assigns the result to $arr.

๐ŸŒ
IONOS
ionos.com โ€บ digital guide โ€บ websites โ€บ web development โ€บ php operators
What are PHP operators in programming? - IONOS
October 18, 2024 - Depending on whether the condition is true or not, weโ€™ll see whether the person is old enough to choose and connect the age to the concatenation operator. Since $age is greater than $legalAge, the condition is true. ... Buying and selling domains can be lucrative โ€“ if you know how to go about it. We willโ€ฆ ... A PHP array is versatile, accommodating various data types like numbers, strings, and objects.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ home โ€บ php โ€บ php operator types
PHP Operator Types
May 26, 2007 - Explore the different types of operators in PHP including arithmetic, assignment, comparison, increment/decrement, and logical operators.