1

我想做这样的事情

$x = 630;
$y = 10;

while ($y < $x){
// do something
$y+10;
}

当我使用$y++它时它正在工作并添加+1,但使用+10 它不起作用。但我需要走+10步。任何指针?

4

3 回答 3

2

在您的代码中,您没有递增$y$y+10返回$yplus的值10,但您需要将其分配给$y.

您可以通过多种方式做到这一点:

  • $y = $y + 10;
  • $y += 10;

例子 :

$x = 630;
$y = 10;
while ($y < $x){
    // do something
    $y = $y + 10;
}
于 2013-01-03T19:18:04.977 回答
1

这是因为 $y++ 等价于 $y = $y + 1; 您没有在 $y 中分配新值。请试试

$y += 10;

或者

$y = $y + 10;
于 2013-01-03T19:20:16.750 回答
0
// commenting the code with description
$x = 630; // initialize x
$y = 10;  // initialize y

while ($y < $x){ // checking whether x is greater than y or not. if it is greater enter loop
// do something
$y = $y+10; // you need to assign the addition operation to a variable. now y is added to 10 and the result is assigned to y. please note that $y++ is equivalent to $y = $y + 1
}
于 2013-01-03T19:22:34.407 回答