1
$var = compact('var1', 'var2') + (object)array(
  'var3' => 'asdf',
  'var4' => 'asdf'
);

doesn't work but why is that sometimes it works? like with

$var = array('var1' => 'asdf', 'var2' => 'asdf') + array('var3' => 'asdf', 'var4' => 'asdf');
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
thelolcat
  • 10,995
  • 21
  • 60
  • 102
  • 3
    What are you using the `+` operator for here? `+` is the mathematical addition operator, or the array union operator.... this doesn't look like mathematical addition to me, or a union of two arrays.... besides the fact that these aren't numeric values for addition, they aren't even the same datatype for array union – Mark Baker Nov 06 '15 at 17:15
  • @MarkBaker I think `+` is also syntactic sugar for `array_merge()`. Question is why not to use the proper function – kero Nov 06 '15 at 17:17
  • @kingkero - not a syntactic sugar, it's subtly different to an array merge – Mark Baker Nov 06 '15 at 17:18
  • @MarkBaker I just realized that myself. [More info here](http://stackoverflow.com/questions/5394157/whats-the-difference-between-array-merge-and-array-array) (for future readers) – kero Nov 06 '15 at 17:19
  • 1
    Under what circumstances does it work/fail? Why are you casting the array to an object? –  Nov 06 '15 at 17:23
  • It will work as a union operator with two arrays, but not with an array and an object – Mark Baker Nov 06 '15 at 17:29

0 Answers0