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的结果输出为6,因为赋值给$y后,$x自己又把自己进行了+1操作。所以,$x的结果为6 echo $x; ?>Let’s compare adding first and then assigning value, as follows:
<?php $x = 5; //先将$x自加1,$x等于5,自加1后结果为6。因此,$y的结果为6 //自加后,再将结果6偷偷的赋值给自己$x $y = ++$x; //$x的结果输出也为6。因为$x执行+1完成后,将5+1的结果赋值给了自己 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; ?>