2014-05-02 105 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;