3v4l.org

run code in 300+ PHP versions simultaneously
<?php error_reporting(-1); class Room { private $number; private $countOfGuests; private $price; private $arrivalDate; private $dateOfDeparture; private $guests = array("firstName" => array(), "lastName" => array()); private $isFree = TRUE; public function __construct(int $number, int $countOfGuests, int $price, DateTime $arrivalDate, DateTime $dateOfDeparture) { $this->number = $number; $this->price = $price; $this->countOfGuests = $countOfGuests; $this->arrivalDate = $arrivalDate; $this->dateOfDeparture = $dateOfDeparture; } public function getIsFree(): bool { return $this->isFree; } public function changeIsFtee():void { if ($this->isFree == TRUE){ $this->isFree = FALSE; } else { $this->isFree = TRUE; } } public function getArrivalDate(): DateTime { return $this->arrivalDate; } public function getDateOfDeparture(): DateTime { return $this->dateOfDeparture; } public function addGuestToRoom($firstName, $lastName): void { $this->guests["firstName"][] = $firstName; $this->guests["lastName"][] = $lastName; } public function getPrice(): int { return $this->price; } public function getCountOfGuests(): int { return $this->countOfGuests; } public function getGuests(): array { return $this->guests; } public function getNumber(): int { return $this->number; } public function setDate(Hotel $hotel, DateTime $arrivalDate, DateTime $dateOfDeparture): void { $this->arrivalDate = $arrivalDate; $this->dateOfDeparture = $dateOfDeparture; } } class Hotel { private $rooms = array(); private $guests = array(); private $receipts = array("date" => array(), "money" => array()); public function addRoomForHotel(int $countOfGuests, int $price): void { $this->rooms[] = new Room(count($this->rooms) + 1, $countOfGuests, $price, new DateTime(), new DateTime()); } public function addGuest(string $firstName, string $lastName): void { $this->guests[] = new Guest($firstName, $lastName); } public function getRooms(): array { return $this->rooms; } public function getRoom(int $numberOfRoom): Room { return $this->rooms[$numberOfRoom - 1]; } public function getGuest(string $firstName, string $lastName): Guest { foreach ($this->guests as $guest) { if ($guest->getFirstName() == $firstName && $guest->getLastName() == $lastName){ return $guest; break; } } if (1){ throw new Exception("Неправильно введены данные клиента"); } } public function getReceipts(): array { return $this->receipts; } public function addDateOfReceipts(DateTime $date): void { $this->receipts["date"][] = $date; } public function addMoneyOfReceipts(int $money): void { $this->receipts["money"][] = $money; } public function replenishMoney(int $money, int $numberInArray): void { $this->receipts["money"][$numberInArray] += $money; } } class Guest { private $firstName; private $lastName; private $visitedRooms = array(); public function __construct(string $firstName, string $lastName) { $this->firstName = $firstName; $this->lastName = $lastName; } public function addVisitedRoom(int $numberOfRoom): void { $this->visitedRooms[] = $numberOfRoom; } public function getFirstName(): string { return $this->firstName; } public function getLastName(): string { return $this->lastName; } public function getVisitedRooms(): array { return $this->visitedRooms; } } class Registration { public function getStatisticOfReceipts(Hotel $hotel, DateTime $startDate, DateTime $endDate): void { echo "Дата Сумма\n"; foreach ($hotel->getReceipts()["date"] as $key => $date) { if($date == $startDate || $date > $startDate){ for($i = $key; $hotel->getReceipts()["date"][$i] != $endDate; $i++){ echo $hotel->getReceipts()["date"][$i]->format('Y-m-d'), " ", $hotel->getReceipts()["money"][$i], "\n"; if($hotel->getReceipts()["date"][$i] == end($hotel->getReceipts()["date"])){ break; } } } break; } } public function registerGuests(Hotel $hotel, int $numberOfRoom, string $firstName, string $lastName, DateTime $arrivalDate, DateTime $dateOfDeparture): void { $hotel->getRoom($numberOfRoom)->setDate($hotel, $arrivalDate, $dateOfDeparture); $hotel->getRoom($numberOfRoom)->addGuestToRoom($firstName, $lastName); $hotel->getRoom($numberOfRoom)->changeIsFtee(); $hotel->getGuest($firstName, $lastName)->addVisitedRoom($numberOfRoom); $this->addReceiptsForHotel($hotel, $numberOfRoom, $arrivalDate); } public function selectRoom(Hotel $hotel, DateTime $arrivalDate, DateTime $dateOfDeparture, int $countOfGuests): void { $availableRoom = $this->searchRooms($hotel, $dateOfDeparture, $arrivalDate); $roomsForList = array(); while ($countOfGuests > 0) { $maxCountOfGuestInRoom = $availableRoom[0]->getCountOfGuests(); foreach ($availableRoom as $room) { if ($room->getCountOfGuests() > $maxCountOfGuestInRoom && $room->getCountOfGuests() <= $countOfGuests){ $maxCountOfGuestInRoom = $room->getCountOfGuests(); } } $roomsForList[] = $this->SearchSuitableRoom($hotel, $arrivalDate, $dateOfDeparture, $maxCountOfGuestInRoom); end($roomsForList)->changeIsFtee(); $countOfGuests -= $maxCountOfGuestInRoom; } $this->printInformationOfRooms($roomsForList); foreach ($roomsForList as $room) { $room->changeIsFtee(); } } public function getFreeRoomsInRange(Hotel $hotel, DateTime $arrivalDate, DateTime $dateOfDeparture):void { $this->printInformationOfRooms($this->searchRooms($hotel, $dateOfDeparture, $arrivalDate)); } public function getFreeRooms(Hotel $hotel, DateTime $date): void { $this->printInformationOfRooms($this->searchRooms($hotel, $date, $date)); } private function searchRooms(Hotel $hotel, DateTime $date1, DateTime $date2) { $freeRooms = array(); foreach ($hotel->getRooms() as $room) { if ($room->getIsFree() && ($room->getArrivalDate() > $date1 || $room->getDateOfDeparture() < $date2)){ $freeRooms[] = $room; } } return $freeRooms; } private function SearchSuitableRoom(Hotel $hotel, DateTime $arrivalDate, DateTime $dateOfDeparture, int $countOfGuests): Room { $availableRoom = $this->searchRooms($hotel, $dateOfDeparture, $arrivalDate); foreach ($availableRoom as $number => $room) { if ($room->getCountOfGuests() < $countOfGuests){ unset($availableRoom[$number]); } } sort($availableRoom); if (count($availableRoom) == 0){ throw new Exception("Нет подходящих номеров"); } $minPrice = $availableRoom[0]->getPrice(); foreach ($availableRoom as $number => $room) { if ($room->getPrice() < $minPrice){ $minPrice = $room->getPrice(); for ($i = 0; $i < $number; $i++){ unset($availableRoom[$i]); } } elseif ($room->getPrice() > $minPrice){ unset($availableRoom[$number]); } } sort($availableRoom); $minGuests = $availableRoom[0]->getCountOfGuests(); foreach ($availableRoom as $number => $room) { if ($room->getCountOfGuests() < $minGuests){ $minGuests = $room->getCountOfGuests(); for ($i = 0; $i < $number; $i++){ unset($availableRoom[$i]); } } elseif ($room->getCountOfGuests() > $minGuests){ unset($availableRoom[$number]); } } sort($availableRoom); return $availableRoom[0]; } public function printHistoryOfGuest(Hotel $hotel, string $firstName, string $lastName): void { echo "\n$firstName $lastName История\n"; $rooms = $hotel->getGuest($firstName, $lastName)->getVisitedRooms(); foreach ($rooms as &$room) { $room = $hotel->getRoom($room); } $this->printInformationOfRooms($rooms); } public function printHistoryOfRoom(Hotel $hotel, int $numberOfRoom): void { echo "\nИстория комнаты № $numberOfRoom\n\n"; echo "Имя Фамилия\n"; $guests = $hotel->getRoom($numberOfRoom)->getGuests(); for($i = 0; $i < count($guests["firstName"]); $i++){ echo $this->padLeft($guests["firstName"][$i], 10) . $this->padLeft($guests["lastName"][$i], 10) . "\n"; } } private function printInformationOfRooms(array $rooms): void { echo "\nНомер вместимость стоимость\n"; foreach($rooms as $room){ $this->printRoomsRow($room->getNumber(), $room->getCountOfGuests(), $room->getPrice()); } } private function padLeft($value, int $columnLength): void { echo $value; echo str_repeat(" ", $columnLength - mb_strlen($value)); } private function printRoomsRow(int $numberOfRoom, int $countOfGuests, int $price): void { $col1 = 6; $col2 = 12; $col3 = 20; echo $this->padLeft($numberOfRoom, $col1) . $this->padLeft($countOfGuests, $col2) . $this->padLeft($price, $col3) . "\n"; } private function addReceiptsForHotel(Hotel $hotel, int $numberOfRoom, DateTime $arrivalDate): void { foreach ($hotel->getReceipts()["date"] as $key => $date) { if ($arrivalDate == $date){ $hotel->replenishMoney($hotel->getRoom($numberOfRoom)->getPrice(), $key); return; } } $hotel->addDateOfReceipts($arrivalDate); $hotel->addMoneyOfReceipts($hotel->getRoom($numberOfRoom)->getPrice()); } } $hotel = new Hotel; $hotel->addRoomForHotel(1, 500); $hotel->addRoomForHotel(3, 700); $hotel->addRoomForHotel(2, 1500); $hotel->addRoomForHotel(2, 1500); $hotel->addRoomForHotel(3, 1500); $hotel->addGuest("Ivan", "Ivanov"); $hotel->addGuest("Petr", "Petrov"); $registration = new Registration; $registration->registerGuests($hotel, 3, "Ivan", "Ivanov", new DateTime('2015-05-10'), new DateTime('2015-05-22')); $registration->registerGuests($hotel, 1, "Petr", "Petrov", new DateTime('2015-05-10'), new DateTime('2015-05-12')); $registration->registerGuests($hotel, 4, "Ivan", "Ivanov", new DateTime('2015-05-27'), new DateTime('2015-06-03')); $registration->registerGuests($hotel, 3, "Petr", "Petrov", new DateTime('2015-07-13'), new DateTime('2015-07-15')); // $registration->getFreeRooms($hotel, new DateTime('2015-05-12')); // $registration->getFreeRoomsInRange($hotel, new DateTime('2015-05-13'), new DateTime('2015-05-25')); // $registration->selectRoom($hotel, new DateTime('2015-05-12'), new DateTime('2015-05-25'), 4); // $registration->printHistoryOfGuest($hotel, "Ivan", "Ivanov"); // $registration->printHistoryOfRoom($hotel, 3); // $registration->getStatisticOfReceipts($hotel, new DateTime('2015-05-05'), new DateTime('2015-07-25'));

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.70.0120.00317.00
8.3.60.0110.01118.63
8.3.50.0110.00716.79
8.3.40.0110.00419.21
8.3.30.0110.00419.04
8.3.20.0050.00322.07
8.3.10.0040.00423.54
8.3.00.0040.00422.10
8.2.190.0060.01318.41
8.2.180.0130.00316.50
8.2.170.0100.00722.96
8.2.160.0120.00322.25
8.2.150.0000.00824.18
8.2.140.0000.00924.66
8.2.130.0030.00521.25
8.2.120.0050.00326.35
8.2.110.0060.00322.11
8.2.100.0060.00620.57
8.2.90.0040.00419.89
8.2.80.0040.00418.29
8.2.70.0030.00618.18
8.2.60.0030.00618.34
8.2.50.0030.00618.10
8.2.40.0050.00521.20
8.2.30.0040.00418.42
8.2.20.0050.00318.32
8.2.10.0040.00418.32
8.2.00.0000.00818.41
8.1.280.0140.00725.92
8.1.270.0000.00823.90
8.1.260.0040.00426.35
8.1.250.0080.00028.09
8.1.240.0030.00623.96
8.1.230.0090.00319.10
8.1.220.0000.00917.91
8.1.210.0080.00019.29
8.1.200.0060.00317.73
8.1.190.0040.00717.76
8.1.180.0030.00818.10
8.1.170.0040.00420.91
8.1.160.0030.00519.11
8.1.150.0030.00622.05
8.1.140.0030.00517.98
8.1.130.0040.00419.25
8.1.120.0050.00617.81
8.1.110.0030.00517.73
8.1.100.0040.00417.88
8.1.90.0050.00317.86
8.1.80.0040.00417.69
8.1.70.0050.00217.84
8.1.60.0030.00617.89
8.1.50.0040.00417.77
8.1.40.0000.00817.86
8.1.30.0000.00918.02
8.1.20.0040.00417.96
8.1.10.0080.00017.81
8.1.00.0030.00617.70
8.0.300.0000.00921.68
8.0.290.0040.00417.25
8.0.280.0000.00818.77
8.0.270.0040.00417.43
8.0.260.0000.00717.17
8.0.250.0030.00517.22
8.0.240.0020.00517.43
8.0.230.0000.00917.38
8.0.220.0000.00717.36
8.0.210.0070.00017.32
8.0.200.0040.00417.42
8.0.190.0040.00417.43
8.0.180.0000.00817.23
8.0.170.0000.00917.21
8.0.160.0040.00417.29
8.0.150.0000.00817.23
8.0.140.0040.00417.29
8.0.130.0060.00013.59
8.0.120.0040.00417.18
8.0.110.0050.00317.20
8.0.100.0000.00817.34
8.0.90.0080.00017.39
8.0.80.0130.01017.31
8.0.70.0040.00417.34
8.0.60.0030.00617.34
8.0.50.0030.00517.15
8.0.30.0080.01017.44
8.0.20.0120.00817.62
8.0.10.0000.00817.44
8.0.00.0120.00617.18
7.4.330.0000.00515.55
7.4.320.0000.00716.91
7.4.300.0050.00316.79
7.4.290.0000.00716.79
7.4.280.0000.00816.70
7.4.270.0040.00416.84
7.4.260.0000.00716.77
7.4.250.0050.00216.77
7.4.240.0000.00716.88
7.4.230.0070.00016.80
7.4.220.0080.00016.73
7.4.210.0130.00116.85
7.4.200.0070.00016.97
7.4.160.0080.01016.88
7.4.140.0100.01217.86
7.4.130.0070.01116.77
7.4.120.0140.00616.86
7.4.110.0030.01516.88
7.4.100.0030.01616.86
7.4.90.0040.01416.84
7.4.80.0170.00519.39
7.4.70.0100.00716.71
7.4.60.0130.01016.89
7.4.50.0060.01016.72
7.4.40.0070.01116.86
7.4.00.0080.00815.49
7.3.330.0060.00013.50
7.3.320.0000.00613.64
7.3.310.0040.00416.70
7.3.300.0040.00416.62
7.3.290.0070.00016.61
7.3.280.0090.00816.64
7.3.260.0120.01016.76
7.3.240.0090.00816.77
7.3.230.0120.00917.00
7.3.210.0140.00416.87
7.3.200.0100.00616.59
7.3.190.0070.01716.89
7.3.180.0150.00917.00
7.3.170.0080.01116.79
7.3.160.0200.00316.88
7.3.10.0030.01316.45
7.3.00.0030.01416.46
7.2.330.0110.00717.03
7.2.320.0170.00717.13
7.2.310.0100.00716.77
7.2.300.0070.01016.84
7.2.290.0150.00616.87
7.2.130.0030.01317.11
7.2.120.0040.00817.27
7.2.110.0100.00317.18
7.2.100.0000.00917.21
7.2.90.0030.01216.91
7.2.80.0070.00717.21
7.2.70.0000.01217.20
7.2.60.0030.00917.04
7.2.50.0060.00617.14
7.2.40.0080.00816.99
7.2.30.0140.01218.42
7.2.20.0110.01118.50
7.2.10.0190.01018.99
7.2.00.0110.01019.19
7.1.250.0030.00715.82
7.1.200.0110.00315.73
7.1.150.0160.01717.99
7.1.140.0150.01217.87
7.1.130.0100.01218.90
7.1.120.0100.01418.93
7.1.110.0150.01118.10
7.1.100.0090.01218.09
7.1.90.0120.01118.05
7.1.80.0090.01418.13
7.1.70.0080.01417.26
7.1.60.0280.02135.28
7.1.50.0250.01534.84
7.1.40.0260.01934.54
7.1.30.0320.01934.66
7.1.20.0290.01834.73
7.1.10.0090.01216.79
7.1.00.0080.01316.85

preferences:
59.74 ms | 401 KiB | 5 Q