0

The code outputs "I will be going to Los Angeles. The journey will be a distance of 300 miles. We will need to make 2 stop." when it should pull the plural or singular of the word "stop" depending on the number defined in my IF statement (in that case, it should be the plural). I'm guessing that the IF doesn't work as I expected it to because of the way I have constructed the class. Any help fixing it would be appreciated.

class trip {

  public $destination;
  public $distance;
  public $unit;
  public $stops = null;
  public $transport = null;

  function __construct($destination, $distance, $unit, $stops, $transport) {
    $this->destination = $destination;
    $this->distance = $distance;
    $this->unit = $unit;
    $this->stops = $stops;
    $this->transport = $transport;
  }

  function getTrip() {
    $a = "I will be going to " . $this->destination . ". ";
    $a .= "The journey will be a distance of " . $this->distance . " " . $this->unit . ". ";
    $a .= "We will need to make " . $this->stops;
    if ($this->stops = 1) {
      $a .= " stop.";
    } else {
      $a .= " stops.";
    }
    $a .= "\n";
    echo $a;
  }
}

$first = new trip('Los Angeles',300,'miles',2,'airplane');
$first->getTrip();
Andy
  • 1,422
  • 5
  • 27
  • 43
  • Different questions which happen to have the same answer aren't duplicate. Unnecessary moderation for the sake of scoring a few extra points is damaging to the Stackoverflow product. – Andy Feb 19 '17 at 22:16

2 Answers2

5

You seem to have overlooked a small detail.

if ($this->stops = 1) 

should be

if ($this->stops == 1) 
NotABlueWhale
  • 795
  • 7
  • 18
2

your IF statement seems wrong. In fact, it should be:

if( $this->stops == 1){ ...

You have to use two == signs to make a comparison, otherwise is an assignment. So in your case, one = sign will not really test the condition you expect.