} } $porsche = new Car(); $porsche->setMakeModel("2014 Porsche 911"); $porsche->setPrice(114991); $porsche->setMiles(7864); $ford = new Car(); $ford->setMakeModel("2011 Ford F450"); $ford->setPrice(55995); $ford->setMiles(14241); $lexus = new Car(); $lexus->setMakeModel("2013 Lexus RX 350"); $lexus->setPrice(44700); $lexus->setMiles(20000); $mercedes = new Car(); $mercedes->setMakeModel("Mercedes Benz CLS550"); $mercedes->setPrice(39900); $mercedes->setMiles(37979); $cars = array($porsche, $ford, $lexus, $mercedes); $cars_matching_search = array(); foreach ($cars as $car) { if ($car->worthBuying($_GET['price']) && $car->maxMiles($_GET['miles'])) { array_push($cars_matching_search, $car); } } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8">
} function worthBuying($max_price) { if ($this->price <= $max_price) { return true; } } } $porsche = new Car("2014 Porshe 911", 110000, 7864); $subaru = new Car("2002 Subaru Outback", 3800, 128000); $toyota = new Car("1994 Toyota Tacoma", 5000, 195000); $ford = new Car("1997 Ford Wrangler", 1000, 300000); $porsche->setMakeModel("2014 Porsche 911"); $subaru->setMakeModel("2002 Subaru Forester"); $toyota->setMakeModel("1994 Toyota Tacoma"); $ford->setMakeModel("1997 Ford Wrangler"); $porsche->setPrice(110000.07); $subaru->setPrice(3400.34); $toyota->setPrice(5000.99); $ford->setPrice(1001.19); $cars = array($porsche, $subaru, $toyota, $ford); $cars_matching_array = array(); foreach ($cars as $car) { if ($car->worthBuying($_GET["price"])) { array_push($cars_matching_array, $car); } } //for each car in our cars array, we will run the "worthBuying" function on it. //if the function returns true, then it will push that car into the cars_matching_array. ?>