3v4l.org

run code in 300+ PHP versions simultaneously
<?php abstract class AbstractQuestion { public $text; public $points; public $hint; public $correctAnswer; abstract function checkAnswer($answer); abstract function printQuestion(); } class ChoiceQuestion extends AbstractQuestion { public $answers = array(); public function __construct(string $text, array $answers, string $correctAnswer, string $hint, int $points) { $this->text = $text; $this->answers = $answers; $this->correctAnswer = $correctAnswer; $this->hint = $hint; $this->points = $points; } public function printQuestion() { echo $this->text."\n"; echo "Варианты ответа:\n"; foreach ($this->answers as $key => $answer) { echo "$key. $answer\n"; } echo "\n"; } public function checkAnswer($answer) { if ($answer == $this->correctAnswer) { return TRUE; } else { return FALSE; } } } class NumericQuestion extends AbstractQuestion { public $deviation; public function __construct(string $text, int $correctAnswer, int $deviation, string $hint, int $points) { $this->text = $text; $this->correctAnswer = $correctAnswer; $this->deviation = $deviation; $this->hint = $hint; $this->points = $points; } public function printQuestion() { echo $this->text, "\n\n"; } public function checkAnswer($answer) { if ($answer <= $this->correctAnswer + $this->deviation && $answer >= $this->correctAnswer - $this->deviation) { return TRUE; } else { return FALSE; } } } function printQuestions(array $questions) { foreach ($questions as $key => $question) { echo $key + 1; echo ". "; $question->printQuestion(); } } function checkAnswers (array $answers, array $questions) { $maxPoints = 0; $points = 0; $countOfCorrentAnswers = 0; $maxAnswers = 0; foreach ($questions as $key => $question) { if ($question->checkAnswer($answers[$key]) == TRUE) { $points += $question->points; $countOfCorrentAnswers++; } else { echo "Неправильный ответ на вопрос №"; echo $key + 1; echo " ($question->text)\n"; echo "Подсказка: $question->hint\n"; } $maxPoints += $question->points; $maxAnswers++; } echo "\nПравильных ответов: $countOfCorrentAnswers из $maxAnswers, баллов набрано: $points из $maxPoints"; } $q1 = new ChoiceQuestion ("Какая планета располагается четвертой по счету от Солнца?", array('a' => 'Венера', 'b' => 'Марс', 'c' => 'Юпитер', 'd' => 'Меркурий'), 'b', "Красная планета", 10); $q2 = new ChoiceQuestion ("Какой город является столицей Великобритании?", array('a' => 'Париж', 'b' => 'Москва', 'c' => 'Нью-Йорк', 'd' => 'Лондон'), 'd', "Там ещё королева есть", 5); $q3 = new NumericQuestion ("Чему равна скорость света в км/с?", 300000, 1000, "Отсоси у тракториста", 10); $q4 = new ChoiceQuestion ("Кто придумал теорию относительности?", array('a' => 'Джон Леннон', 'b' => 'Джим Моррисон', 'c' => 'Альберт Эйнштейн', 'd' => 'Исаак Ньютон'), 'c', "Показывал язык", 30); $questions = array($q1, $q2, $q3, $q4); printQuestions($questions); $answers = array('b', 'c', 299000, 'c'); checkAnswers($answers, $questions);
Output for 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
1. Какая планета располагается четвертой по счету от Солнца? Варианты ответа: a. Венера b. Марс c. Юпитер d. Меркурий 2. Какой город является столицей Великобритании? Варианты ответа: a. Париж b. Москва c. Нью-Йорк d. Лондон 3. Чему равна скорость света в км/с? 4. Кто придумал теорию относительности? Варианты ответа: a. Джон Леннон b. Джим Моррисон c. Альберт Эйнштейн d. Исаак Ньютон Неправильный ответ на вопрос №2 (Какой город является столицей Великобритании?) Подсказка: Там ещё королева есть Правильных ответов: 3 из 4, баллов набрано: 50 из 55
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 1. Какая планета располагается четвертой по счету от Солнца? Варианты ответа: a. Венера b. Марс c. Юпитер d. Меркурий 2. Какой город является столицей Великобритании? Варианты ответа: a. Париж b. Москва c. Нью-Йорк d. Лондон 3. Чему равна скорость света в км/с? 4. Кто придумал теорию относительности? Варианты ответа: a. Джон Леннон b. Джим Моррисон c. Альберт Эйнштейн d. Исаак Ньютон Неправильный ответ на вопрос №2 (Какой город является столицей Великобритании?) Подсказка: Там ещё королева есть Правильных ответов: 3 из 4, баллов набрано: 50 из 55

preferences:
150.35 ms | 404 KiB | 156 Q