Home > Backend Development > PHP Tutorial > PHP中$a=1;$b=&$a;$c=(++$a)+(++$a)+(++$a); $c为啥等于10?

PHP中$a=1;$b=&$a;$c=(++$a)+(++$a)+(++$a); $c为啥等于10?

WBOY
Release: 2016-06-17 08:32:26
Original
1608 people have browsed it

回复内容:

别讨论未定义行为了好不

PHP: Operator Precedence 首先需要了解一下php源码有关引用计数,变量分离的知识。然后我们看下opcode:

<code class="language-text">line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   3     0  E >   ASSIGN                                                   !0, 1
   4     1        ASSIGN_REF                                               !1, !0
   5     2        PRE_INC                                          $2      !0
         3        PRE_INC                                          $3      !0
         4        ADD                                              ~4      $2, $3
         5        PRE_INC                                          $5      !0
         6        ADD                                              ~6      ~4, $5
         7        ASSIGN                                                   !2, ~6
   6     8        SEND_VAR                                                 !2
         9        DO_FCALL                                      1          'var_dump'
        10      > RETURN                                                   1
</code>
Copy after login
这可以说是PHP7以下版本的bug 怎么没人解释?难道7跟5就着点区别? php7中,$a=1;$b=&$a;$c=(++$a)+(++$a)+(++$a);结果$c是9。php5中,结果就是10. php5中:
$a=1;$b=&$a;$c=(++$a)+(++$a)+(++$a); //$c==10
$a=1;$b=&$a;$c=(++$a); //$c==2
$a=1;$b=&$a;$c=(++$a)+(++$a); //$c==6
php7里$a=1;$b=&$a;$c=(++$a)+(++$a)+(++$a);结果$c是9。
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template