In PHP, $ a = 1; $ B = & $ a; $ c = (++ $ a) + (++ $ ); why is $ c equal to 10?

Source: Internet
Author: User
Php Chinese network (www.php.cn) provides the most comprehensive basic tutorial on programming technology, introducing HTML, CSS, Javascript, Python, Java, Ruby, C, PHP, basic knowledge of MySQL and other programming languages. At the same time, this site also provides a large number of online instances, through which you can better learn programming... Reply: Do not discuss undefined behaviors.

PHP: Operator Precedence First, you need to know about reference counting and variable separation in the php source code. Then let's take a look at opcode:

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
This can be said to be a bug in PHP 7 or earlier versions. why not explain it? Is 7 different from 5? Php7, $ A = 1; $ B = & $ a; $ c = (++ $ a) + (++ $ ); the result $ c is 9. In php5, the result is 10.Php5:
$ A = 1; $ B = & $ a; $ c = (++ $ a) + (++ $ ); // $ c = 10
$ A = 1; $ B = & $ a; $ c = (++ $ a); // $ c = 2
$ A = 1; $ B = & $ a; $ c = (++ $ a) + (++ $ a); // $ c = 6
In php7, $ a = 1; $ B = & $ a; $ c = (++ $ a) + (++ $ ); the result $ c is 9.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.