为什么我的类中的while循环永远循环?

时间:2014-05-02 18:03:27

标签: php class while-loop

我已经增加了循环并且查看了很多次。当$ change低于0时,循环应该会中断,但它会永远循环,直到它超过30秒的最大时间。我是初学者,所以我很感激你的帮助。

<?php

class change{

private $cash = 0;
private $change = 0;
private $price = 0;
private $counter = 0;

public function setPrice($setPrice){
    $this->price = $setPrice;
 }

public function setCash($setCash){
    $this->cash = $setCash;
 }

public function getChange(){
    $this->change = ($this->cash - $this->price);
    while($this->change >= 0){
    $this->change - 50;
    $this->counter++;
  }
    return $this->counter;

  }
}


$newChange = new change;

$newChange->setCash(600);
$newChange->setPrice(200);

$newChange->getChange();

?>

1 个答案:

答案 0 :(得分:3)

 while($this->change >= 0){
    $this->change - 50;
    $this->counter++;
  }

您需要实际设置$this->change而不是仅执行减法。你可以$this->change -= 50;