我想做这样的事情
$x = 630;
$y = 10;
while ($y < $x){
// do something
$y+10;
}
当我使用$y++
它正在工作并添加+1时,但是+10它不起作用。但我需要进行+10步。有什么指针吗?
答案 0 :(得分:2)
在您的代码中,您没有递增$y
:$y+10
返回$y
加10
的值,但您需要将其指定给$y
您可以通过多种方式实现:
$y = $y + 10;
$y += 10;
示例:
$x = 630;
$y = 10;
while ($y < $x){
// do something
$y = $y + 10;
}
答案 1 :(得分:1)
这是因为$ y ++相当于$ y = $ y + 1;您没有在$ y中分配新值。请尝试
$y += 10;
OR
$y = $y + 10;
答案 2 :(得分: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
}