Question

I've incremented the loop and looked it over a good amount of times. The loop should just break when $change goes below 0, yet it loops forever until it exceeds the maximum time of 30 seconds. I'm a beginner, so I'd appreciate some help please.

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

?>
Was it helpful?

Solution

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

You will need to actually set $this->change instead of just performing a subtraction. You can do $this->change -= 50;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top