What is the accuracy and associativity of the increment operator and assignment operator for a block of code

What is the accuracy and associativity of the increment operator and assignment operator for a block of code

$a=array(1,2,3);
$b=array(4,5,6);
$c=1;

$a[$c++]=$b[$c++];

print_r($a);

In accordance with the implementation, he issues

 Array
       (    
         [0] => 1
         [1] => 6
         [2] => 3
       )

But I can’t understand how the array $aindex 1 contains the value of the array, the $bvalue of index 2. Can someone explain the script how the execution is done?

+4
source share
3 answers

PHP (again) differs from other languages ​​in that the first part of the assignment is evaluated first. Simple proof:

$a[print 1] = $b[print 2]; // what does this print?

According to http://3v4l.org/ , this code:

$a = array(); $b = array(); $c = 1;
$a[$c++]=$b[$c++];

The following operation codes are generated:

compiled vars:  !0 = $a, !1 = $b, !2 = $c
line     # *  op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
   2     0  >   INIT_ARRAY                                       ~0      
         1      ASSIGN                                                   !0, ~0
         2      INIT_ARRAY                                       ~2      
         3      ASSIGN                                                   !1, ~2
         4      ASSIGN                                                   !2, 1
   3     5      POST_INC                                         ~5      !2
         6      POST_INC                                         ~7      !2
         7      FETCH_DIM_R                                      $8      !1, ~7
         8      ASSIGN_DIM                                               !0, ~5
         9      OP_DATA                                                  $8, $9
        10    > RETURN                                                   1

5 - $c++, 6 - $c++. , ( 8)

$a[1] = $b[2];

(1,6,3).

+2

++ post increment , (post) . $c++ $c, $c.

, , :

$a[$c++] =

$c++ 1, $c 2.

$b[$c++]

$c++ 2, $c 3 ( ).

, :

$a[1] = $b[2];

, pre-increment ++$var , . , $a[++$c] = $b[++$c] Undefined 3 $b.

0

All Articles