[development] += on arrays

Karoly Negyesi karoly at negyesi.net
Tue Jan 17 11:45:12 UTC 2006


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


More information about the development mailing list