PHP:使用++或+ = 1之间有区别[关闭]

When using increment on variables in Javascript, lint says that x += 1 is preferred over x ++.

But what about in PHP?

Is there any difference between the += and ++ or does it just not really matter?

Well whatever you might say about conventions, try running the following...

$i = 1;  $s = 's';
$i++;    $s++;
echo $i.'<br>'.$s.'<br>';

$i = 1;   $s = 's';
$i += 1;  $s += 1;
echo $i.'<br>'.$s.'<br>';

the output is somewhat unexpected...

2
t
2
1

so I would say it could matter very much which is chosen!

x += 1 is rather equivalent to ++x.

All those expressions (x += 1, x++ and ++x) increment the value of num by one, but the value of x++ is the value x had before it got incremented.