fork download
  1. <?php
  2. // error_reporting(-1);
  3. class Question
  4. {
  5. public $text; // текст вопроса
  6. public $points = 5; // число баллов, по умолчанию 5
  7. public $answers; // варианты ответов
  8. public $correctAnswer; // правильный ответ
  9.  
  10. }
  11. function createQuestions()
  12. {
  13. $questions = [];
  14.  
  15. $q = New Question;
  16. $q -> $text = 'Столица Россиюшки';
  17. $q -> $points = 1;
  18. $q -> $answers = array('a' => 'Париж', 'b' => 'Москва', 'c' => 'Нью-Йорк', 'd' => 'Лондон');
  19. $q -> $correctAnswer = 'b';
  20.  
  21. $questions[] = $q;
  22.  
  23. $q = New Question;
  24. $q -> $text = 'Ээх, как же хочется в...';
  25. $q -> $points = 1;
  26. $q -> $answers = array('a' => 'Россиюшку', 'b' => 'мамину квартиру', 'c' => 'Магадан', 'd' => 'ЮВА');
  27. $q -> $correctAnswer = 'd';
  28.  
  29. $questions[] = $q;
  30.  
  31. $q = New Question;
  32. $q -> $text = 'Наверное, мы преуспеем в чем?';
  33. $q -> $points = 1;
  34. $q -> $answers = array('a' => 'В прокрастинации', 'b' => 'В программировании', 'c' => 'В прокачивании эльфа 80 уровня', 'd' => 'Не придумал');
  35. $q -> $correctAnswer = 'd';
  36.  
  37. $questions[] = $q;
  38.  
  39. Return $questions;
  40. }
  41.  
  42. function printQuestions($questions)
  43. {
  44. $number = 1;
  45.  
  46. foreach ($questions as $Question) {
  47. echo "{$number}. {$Question->text}\n\n";
  48.  
  49. echo "Варианты ответов:\n";
  50.  
  51. foreach ($questions->answers as $letter => $answers) {
  52. echo " {$letter}. {$answer}\n";
  53. }
  54.  
  55. $number++;
  56. }
  57. }
  58.  
  59. $questions = createQuestions();
  60. printQuestions($questions);
  61. ?>
Success #stdin #stdout #stderr 0.04s 23564KB
stdin
Standard input is empty
stdout
1. 

Варианты ответов:
2. 

Варианты ответов:
3. 

Варианты ответов:
stderr
PHP Notice:  Undefined variable: text in /home/VDhczy/prog.php on line 16
PHP Notice:  Undefined variable: points in /home/VDhczy/prog.php on line 17
PHP Notice:  Undefined variable: answers in /home/VDhczy/prog.php on line 18
PHP Notice:  Undefined variable: correctAnswer in /home/VDhczy/prog.php on line 19
PHP Notice:  Undefined variable: text in /home/VDhczy/prog.php on line 24
PHP Notice:  Undefined variable: points in /home/VDhczy/prog.php on line 25
PHP Notice:  Undefined variable: answers in /home/VDhczy/prog.php on line 26
PHP Notice:  Undefined variable: correctAnswer in /home/VDhczy/prog.php on line 27
PHP Notice:  Undefined variable: text in /home/VDhczy/prog.php on line 32
PHP Notice:  Undefined variable: points in /home/VDhczy/prog.php on line 33
PHP Notice:  Undefined variable: answers in /home/VDhczy/prog.php on line 34
PHP Notice:  Undefined variable: correctAnswer in /home/VDhczy/prog.php on line 35
PHP Notice:  Trying to get property of non-object in /home/VDhczy/prog.php on line 51
PHP Warning:  Invalid argument supplied for foreach() in /home/VDhczy/prog.php on line 51
PHP Notice:  Trying to get property of non-object in /home/VDhczy/prog.php on line 51
PHP Warning:  Invalid argument supplied for foreach() in /home/VDhczy/prog.php on line 51
PHP Notice:  Trying to get property of non-object in /home/VDhczy/prog.php on line 51
PHP Warning:  Invalid argument supplied for foreach() in /home/VDhczy/prog.php on line 51