PHP basic syntax: self-increment and self-decrement
Self-adding and self-subtracting operations
Self-adding and self-subtracting means adding 1 to yourself or subtracting 1 from yourself.
If you have studied other programming languages. You will find that the usage here is another rule in the computer. It can be used like this, which makes it more concise.
Symbol | Description |
---|---|
First Add | |
Assign value first and then subtract | |
Add first and then assign value | |
Decrease first and then assign value |
<?php $x = 5; //先賦值后加:即先將$x的值賦值給$y。$x的值為5,所以將$x的值賦值給$y。$y也為5 $y = $x++; //$x的結(jié)果輸出為6,因?yàn)橘x值給$y后,$x自己又把自己進(jìn)行了+1操作。所以,$x的結(jié)果為6 echo $x; ?>Let’s compare adding first and then assigning value, as follows:
<?php $x = 5; //先將$x自加1,$x等于5,自加1后結(jié)果為6。因此,$y的結(jié)果為6 //自加后,再將結(jié)果6偷偷的賦值給自己$x $y = ++$x; //$x的結(jié)果輸出也為6。因?yàn)?x執(zhí)行+1完成后,將5+1的結(jié)果賦值給了自己 echo $x; ?>You can do another experiment and try $x-- and --$x. Is this the result? Please answer, what is the result of $water + $paper below?
<?php $x = 5; $y = 6; $foo = $x++ + $x--; $bar = ++$y + ++$x; $cup = $x-- + $y--; $paper = ++$x + $x++; $water = $y-- + $x--; echo $water + $paper; ?>