Php's most basic ++ a and a ++ increments

Source: Internet
Author: User
The most basic php ++ a and a ++ increments & lt ;? Php & nbsp; $ a = 3; & nbsp; echo & nbsp; $ a ++. '& lt; br & gt;'; // 3 & nbsp; & nbsp php's most basic ++ a and a ++ increments
$ A = 3;
Echo $ a ++ .'
'; // 3
Echo ++ $ a; // 5
?>
A ++ is first assigned and then added
++ A is first added and then assigned

Who can tell me why? I will give it to him at once.
------ Solution --------------------
Echo $ a ++ is equivalent to echo $ a; $ a + = 1;
Echo ++ $ a is equivalent to: $ a + = 1; echo $;

That is to say, $ a ++ uses the variable value first and then auto-increment.
++ $ A is the first auto-incrementing value and then used.
------ Solution --------------------
Reference:
Can this be an analogy?
That is to say
$ A = 1
Echo $ a ++ // that is, 1 ++ ??? Equal to 1
Echo $ ++ a // ++ 1 ++ equals 3

What you finally understand is wrong. after the first two steps are completed, $ a is equal to 2. Therefore, the last step is directly ++ 1, and there will be no ++ in the end.
Let's look at the example below.


$ A = 1;

Echo $ a ++; // output 1 first outputs $ a and then executes $ a ++. Therefore, the echo $ a below is 2.

Echo $ a; // output 2
// $ A is equal to 2 at this time
Echo ++ $ a; // output 3 ++ $ a. execute the ++ operation, that is, 3, and then output $.


------ Solution --------------------

$ A = 3;
// Echo $ a ++ .'
';
// The above sentence can be split
Echo $ a; // 3 output first
$ A + 1; // returns $ a = 4.

// Echo ++ $;
// The above sentence can be split
$ A + 1; // first calculate $ a = 4 + 1 get 5
Echo $ a; // 5 and then output
?>

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.