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 git.master, git.master_jit, rfc.property-hooks
1. Какая планета располагается четвертой по счету от Солнца? Варианты ответа: a. Венера b. Марс c. Юпитер d. Меркурий 2. Какой город является столицей Великобритании? Варианты ответа: a. Париж b. Москва c. Нью-Йорк d. Лондон 3. Чему равна скорость света в км/с? 4. Кто придумал теорию относительности? Варианты ответа: a. Джон Леннон b. Джим Моррисон c. Альберт Эйнштейн d. Исаак Ньютон Неправильный ответ на вопрос №2 (Какой город является столицей Великобритании?) Подсказка: Там ещё королева есть Правильных ответов: 3 из 4, баллов набрано: 50 из 55

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
59.27 ms | 403 KiB | 8 Q