+ for PHP arrays May 28
$a + $b is the same as $b + $a?
No, if $a and $b are arrays.
what does '+' for array arguments? Let's ask man:
The + operator appends the right handed array to the left handed, whereas duplicated keys are NOT overwritten.
Must be understood. If you know how array keys are set by default:
when you are writing something like
If you do not specify a key for a given value, then the maximum of the integer indices is taken, and the new key will be that maximum value + 1. If you specify a key that already has a value assigned to it, that value will be overwritten.
If no integer values are in keys - it sets zero.
This means that you must be very sure about your keys when you use '+' for arrays. Because '+' works basing on keys, not the values.
And how it works with '-'?
Nohow. Sorry, guys. '-' is not defined for PHP arrays.
A little bonus:
Comparison is defined for PHP arrays. Here is how it works:
array vs. array
Array with fewer members is smaller, if key from operand 1 is not found in operand 2 then arrays are uncomparable, otherwise - compare value by valuearray vs. anything
array is always greater
A little note:
'==' and '===' works differently.
yes, both or then checks if arrays have same amount of elements, same keys, and values must be ==/=== (correspondingly) for sure. But '===' also checks the sequence of elements. So
$b = array(1=>1,2=>2,3=>3);
$c = $a==$b; //true
$c = $a===$b; //false
//
$a = array(1=>1,3=>3,2=>2);
$b = array(1=>1,2=>2,3=>3);
$c = $a==$b; //true
$c = $a===$b; //false
//
$a = array(1=>1,2=>2,3=>3);
$b = array(1=>1,2=>2,3=>3);
$c = $a==$b; //true
$c = $a===$b; //true