+ + And a little doubt-php Tutorial

Source: Internet
Author: User
+ + And a little doubt $ A = 1;
$ B = & $;
$ B = $ a ++;

// Calculate $ a and $ B
In the second sentence, B also points to the address of;
In the third sentence, the value of a is added with 1, so $ B = 1;
The key is that I don't know $ a ++ later. I don't mean to add 1 after using it, so now $ a = 2;
But the address of $ B is the same as the address of a. isn't the address of $ B equal to 2? Or is $ a ++ not executed in the third sentence? The answer to this question is $ a and $ B is 1;
For example
$ A = 1;
$ B = $ a ++;
?>
This is not $ B = 1, $ a = 2
A little dizzy...


Reply to discussion (solution)

$ A = 1;
$ B = $ a ++;

Result:
$ B = 1
$ A = 2;

If $ B = & $;
Indicates that $ a $ B points to the same address.
If $ B changes, the value of $ a also changes, and eventually $ B = 1, $ a = 1

$a = 1;$b = $a++;var_dump($a, $b);/*int(2)int(1)*/$a = 1;$b = &$a;$a++;var_dump($a, $b);/*int(2)int(2)*/$a = 1;$b = &$a;$b = $a++;var_dump($a, $b);/*int(1)int(1)*/

For $ B = $ a ++;
Assign the value 1 of $ a to $ B, but the priority of ++ is higher than =, so the value is not assigned first, but ++, therefore, the value of $ a is 2 (because $ B is a reference of $ a, the value of $ B is also 2)
Then assign a value. The value of $ B is equal to 1.
Because $ B is a reference of $ a, $ a is changed back to 1.

The basics are not well laid ~ Thank you!

If you do not know the operator priority, you can understand it!

   
The image point is that a B represents several apples in the same drawer! James is in charge of taking a look at the drawer where a is located and remembering a few apples in the drawer (that is, what is a), and then placing an apple in the drawer! Then, change the number of apples in the drawer where B is located to the number of just remembered apples, that is, the value of! So change it! It's changed back to!

The first case is not referenced.

$ A = 1; $ B = $ a; // B = 1 a = 1 $ B = $ a ++; // a is assigned to B first, then auto-increment B = 1 a = 2 echo $ a; echo $ B;


In the second case, reference
$ A = 1; $ B = & $ a; // B references a, that is, a, and B points to the same memory address $ B = $ a ++; // a reads the value and executes auto-increment. a = 2 B = 2, and then assigns the value to B. Therefore, B = 1 a = 1 echo $; echo $ B;

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.