2014-05-02 107 views
0

我已经增加了循环,并且查看了很多次。当$ change低于0时,循环应该会中断,但它会一直循环,直到它超过最大时间30秒。我是初学者,所以我希望得到一些帮助。为什么这个while循环在我的类中永远循环?

<?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(); 

?> 
+2

即使你减去50从'$这个 - > change'你是不是把结果存储等等'$这个 - > change'将始终保持不变,每循环迭代 – celeriko

+0

你觉得' $ this-> change - 50;'呢? –

+0

谢谢,初学者的问题通常是在这里看不到的? –

回答

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

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