为什么$ m = 40?

时间:2014-02-25 16:42:37

标签: php operators addition

为什么$ m = 40?我是php的新手,我得到为什么$ k = 40,但我无法弄清楚为什么$ m = 40。

<?php
$i = 29;
$j = 11;
$m = 10;

$k = $i++ + $j--;
echo "k = ".$k."<br>";

$j = ($j - 4) / 2;
$m += $j * 10;
echo "m = ".$m."<br>";
?>

5 个答案:

答案 0 :(得分:1)

<?php
$i = 29;
$j = 11;
$m = 10;

$k = $i++ + $j--;
// $j == 10
echo "k = ".$k."<br>";

$j = ($j - 4) / 2;
// $j = (10 - 4 ) / 2 == 3
// $m == 10
$m += $j * 10;
// $m + $j * 10 = 10 + 3 * 10 == 40
echo "m = ".$m."<br>";
?>

答案 1 :(得分:1)

快速解释发生了什么:

$k = 29 + 11 = 40;
$i = 30 (++)
$j = 10 (--)
echo K = 40
$j = (10-4) / 2 = 3
$m = 3 * 10 + 10 = 40

'+ ='运算符将右边的操作结果($ j * 10)添加到左边的值($ m包含10)

答案 2 :(得分:1)

这是:

$i = 29;
$j = 11;
$m = 10;

$k = $i++ + $j--;

// $k = 40
// $i = 30
// $j = 10

echo "k = ".$k."<br>";

//echo k = 30

$j = ($j - 4) / 2;

// $j = 3

$m += $j * 10;

// $m = 10 + ( 3 * 10 ) = 40

echo "m = ".$m."<br>";

//echo m = 40

答案 3 :(得分:1)

这可能是$ ++和++ $的谜题。

// First try:
$a = 0;
$b = 1;
var_dump($c = $a + $b++); // means: $c = $a + $b; $b = $b + 1;

// Second try:
$a = 0;
$b = 1;
var_dump($c = $a + ++$b); // means: $b = $b + 1; $c = $a + $b;

^有意义吗?

答案 4 :(得分:0)

$j = ($j - 4) / 2; // $j is equal with 10 because of decrement $j-- before
$m += $j * 10; // after that line, $j becomes (10-4)/2 which is 3
echo "m = ".$m."<br>"; // and finally $m = $m + ($j * 10) => 10 + (3 * 10)