Hi! jvandyk raised the question whether += is officially supported by PHP as it is not in the documentation. I contacted Goba who pointed me to lxr.php.net and from there I read the relevant pieces of PHP source code and it's quite clear it does. Stop here if you are not interested in gory details. I said you should stop, right? OK, here are the details if you have not stopped: http://lxr.php.net/search?string=T_PLUS_EQUAL both in Zend and ZendEngine2 we can see that T_PLUS_EQUAL is executed by: zend_do_binary_assign_op(ZEND_ASSIGN_ADD, &$$, &$1, &$3 TSRMLS_CC); so we move on to http://lxr.php.net/search?string=ZEND_ASSIGN_ADD and jump to http://lxr.php.net/source/Zend/zend_execute.c#1162 case ZEND_ASSIGN_ADD: EG(binary_op) = add_function; goto binary_assign_op_addr; and finally we peek into add_function: http://lxr.php.net/source/Zend/zend_operators.c#613 617 if (op1->type == IS_ARRAY && op2->type == IS_ARRAY) { So. We gained the knowledge that it's split into an addition and an assign before the op types are checked. Therefore, if + works for a certain kind of operation, then += does, too. (Zend2 analysis is left as an exercise. It leads to add_function, too.) Regards NK
jvandyk raised the question whether += is officially supported by PHP as it is not in the documentation. I contacted Goba who pointed me to lxr.php.net and from there I read the relevant pieces of PHP source code and it's quite clear it does. !#@%$"!
Stop here if you are not interested in gory details. I said you should stop, right? OK, here are the details if you have not stopped:
http://lxr.php.net/search?string=T_PLUS_EQUAL
both in Zend and ZendEngine2 we can see that T_PLUS_EQUAL is executed by:
zend_do_binary_assign_op(ZEND_ASSIGN_ADD, &$$, &$1, &$3 TSRMLS_CC);
so we move on to http://lxr.php.net/search?string=ZEND_ASSIGN_ADD
RTFM :))) "In addition to the basic assignment operator, there are "combined operators" for all of the binary arithmetic and string operators that allow you to use a value in an expression and then set its value to the result of that expression. For example:" from: http://www.php.net/manual/en/language.operators.assignment.php Sometimes it does pay off reading the manual....
"In addition to the basic assignment operator, there are "combined operators" for all of the binary arithmetic and string operators that
Array is neither binary nor string. The relevant page is http://www.php.net/manual/en/language.operators.array.php imo.
"In addition to the basic assignment operator, there are "combined operators" for all of the binary arithmetic and string operators that
Array is neither binary nor string. The relevant page is http://www.php.net/manual/en/language.operators.array.php imo. partially true - binary here means 2 arguments. Array is a built in type and has a similar treatment to integer, string, ....
It is fair to say that the manual page should be explicit, rather that use deductions, but hey, nobody's perfect.
On Tue, 17 Jan 2006 13:06:14 +0100, vlado <vlado@dikini.net> wrote:
"In addition to the basic assignment operator, there are "combined operators" for all of the binary arithmetic and string operators that
Array is neither binary nor string. The relevant page is http://www.php.net/manual/en/language.operators.array.php imo. partially true - binary here means 2 arguments. Array is a built in type and has a similar treatment to integer, string, ....
"binary arithmetic" means binary arithmetic, for example shift. Not "two operands arithmetic" IMO.
It is fair to say that the manual page should be explicit, rather that use deductions, but hey, nobody's perfect.
I filed a docs bugs on bugs.php.net.
So does this mean that we can do: $form['foo'] = array('a' => 'b', 'c' => 'd'); $form['foo'] += array('a' => 'new b', 'e' => 'f'); instead of: $form['foo'] = array('a' => 'b', 'c' => 'd'); $form['foo'] = array_merge($form['foo'], array('a' => 'new b', 'e' => 'f')); ??? With the intense use of arrays in the new form api that sure would be nice. -jeff
+= is not array_merge. array_merge overwrites keys that are the same while += does not overwrite. (now, that I read PHP source code, I know that $a += $a; for arrays is nicely optimized: the add function immediately returns and does iterate / copy etc the whole array needlessly.) In short: no, because
$form['foo'] = array('a' => 'b', 'c' => 'd'); $form['foo'] += array('a' => 'new b', 'e' => 'f');
would result in array('a' => 'b', 'c' => 'd', 'e' => 'f'); Regards NK
(Probable naivete on my part follows.) So... array_merge($a, $b) favors $a, while $a += $b favors $b? Wouldn't that mean that array_merge($a, $b) == $b += $a Or am I just being dense? :-) On Tuesday 17 January 2006 09:52, Karoly Negyesi wrote:
+= is not array_merge.
array_merge overwrites keys that are the same while += does not overwrite. (now, that I read PHP source code, I know that $a += $a; for arrays is nicely optimized: the add function immediately returns and does iterate / copy etc the whole array needlessly.)
In short: no, because
$form['foo'] = array('a' => 'b', 'c' => 'd'); $form['foo'] += array('a' => 'new b', 'e' => 'f');
would result in array('a' => 'b', 'c' => 'd', 'e' => 'f');
Regards
NK
-- Larry Garfield AIM: LOLG42 larry@garfieldtech.com ICQ: 6817012 "If nature has made any one thing less susceptible than all others of exclusive property, it is the action of the thinking power called an idea, which an individual may exclusively possess as long as he keeps it to himself; but the moment it is divulged, it forces itself into the possession of every one, and the receiver cannot dispossess himself of it." -- Thomas Jefferson
First of all, you should remember this fundamental difference between array_merge() and the array + operator: " Don't forget that numeric keys will be renumbered! ... If you want to completely preserve the arrays and just want to append them to each other, use the + operator." (from http://www.php.net/manual/en/function.array-merge.php) Personally, I find that the re-indexing behaviour of array_merge() is often not what I want, so I'm finding that I use the + operator more and more. Also, consider these two examples: $array1 = array_merge($array1, $array2); $array1 += $array2; In the first example (assuming that the keys aren't numeric), $array2 is guaranteed to have all its values preserved; in the second example (no matter what type the keys are), $array1 is guaranteed this. So... On 1/18/06, Larry Garfield <larry@garfieldtech.com> wrote:
So... array_merge($a, $b) favors $a, while $a += $b favors $b? Wouldn't that mean that
array_merge($a, $b) == $b += $a
It's the other way around, Larry. :-) But the two operations are only equivalent IF there are no numeric keys! If there are, then array_merge() behaves differently (and stupidly, IMO). On a related note: perhaps we should agree on a recommended way (i.e. one of the above two ways) of merging arrays in Drupal? I'd like to see the + operator encouraged, as it's more predictable, and (according to chx) is also faster. But I don't know that we can outlaw array_merge() altogether (as we did with array_key_exists() recently) as there may still be times when it's needed, since it is a bit different to the + operator. Jaza.
On Tuesday 17 January 2006 20:28, Jeremy Epstein wrote:
First of all, you should remember this fundamental difference between array_merge() and the array + operator:
" Don't forget that numeric keys will be renumbered! ... If you want to completely preserve the arrays and just want to append them to each other, use the + operator." (from http://www.php.net/manual/en/function.array-merge.php)
Personally, I find that the re-indexing behaviour of array_merge() is often not what I want, so I'm finding that I use the + operator more and more.
Also, consider these two examples:
$array1 = array_merge($array1, $array2); $array1 += $array2;
In the first example (assuming that the keys aren't numeric), $array2 is guaranteed to have all its values preserved; in the second example (no matter what type the keys are), $array1 is guaranteed this. So...
On 1/18/06, Larry Garfield <larry@garfieldtech.com> wrote:
So... array_merge($a, $b) favors $a, while $a += $b favors $b? Wouldn't that mean that
array_merge($a, $b) == $b += $a
It's the other way around, Larry. :-) But the two operations are only equivalent IF there are no numeric keys! If there are, then array_merge() behaves differently (and stupidly, IMO).
Ah ha. So I was backward but had the right idea. :-) The direction I'm going is...
On a related note: perhaps we should agree on a recommended way (i.e. one of the above two ways) of merging arrays in Drupal? I'd like to see the + operator encouraged, as it's more predictable, and (according to chx) is also faster. But I don't know that we can outlaw array_merge() altogether (as we did with array_key_exists() recently) as there may still be times when it's needed, since it is a bit different to the + operator.
Exactly. If array_merge($a, $b) == $b += $a iff $a and $b are strictly associative arrays, and += is substantially faster, and Drupal uses associative arrays a LOT, it seems like this is a good language-level optimization to make and recommend. -- Larry Garfield AIM: LOLG42 larry@garfieldtech.com ICQ: 6817012 "If nature has made any one thing less susceptible than all others of exclusive property, it is the action of the thinking power called an idea, which an individual may exclusively possess as long as he keeps it to himself; but the moment it is divulged, it forces itself into the possession of every one, and the receiver cannot dispossess himself of it." -- Thomas Jefferson
participants (5)
-
Jeff Robbins -
Jeremy Epstein -
Karoly Negyesi -
Larry Garfield -
vlado