fork download
  1. <?php
  2.  
  3.  
  4. class Question
  5. {
  6. public $text; // текст вопроса
  7. public $points = 5; // число баллов, по умолчанию 5
  8. public $answers; // варианты ответов
  9. public $correctAnswer; // правильный ответ
  10. }
  11. $text = "Какая планета располагается четвертой по счету от Солнца?";
  12. $points = 10;
  13. $answers = array(
  14. 'a' => 'Венера',
  15. 'b' => 'Марс',
  16. 'c' => 'Юпитер',
  17. 'd' => 'Меркурий'
  18. );
  19. $correctAnswer = 'b';
  20.  
  21.  
  22. function createQuestions($text, $points, $answers, $correctAnswer)
  23. {
  24.  
  25. $q = new Question;
  26. $q->text = $text;
  27. $q->points = $points;
  28. $q->answers = $answers;
  29. $q->correctAnswer = $correctAnswer;
  30. $questions[] = $q;
  31. return $q;
  32.  
  33. }
  34. function printQuestions($questions)
  35. {
  36. $i = 1; // номер вопроса
  37.  
  38. foreach ($questions as $question) {
  39. echo "{$i}. {$question->text}\n\n";
  40.  
  41. echo "Варианты ответов:\n";
  42.  
  43. foreach ($question->answers as $letter => $answer) {
  44. echo " {$letter}. {$answer}\n";
  45. }
  46.  
  47. $i++;
  48. }
  49. } {
  50.  
  51. }
  52. $questions = array();
  53. $questions[] = createQuestions($text, $points, $answers, $correctAnswer);
  54. var_dump($questions);
  55.  
  56. $text2 = 'Какой город является столицей Великобритании?';
  57. $points2 = 6;
  58. $answers2 = array(
  59. 'a' => 'Париж',
  60. 'b' => 'Москва',
  61. 'c' => 'Нью-Йорк',
  62. 'd' => 'Лондон'
  63. );
  64. $correctAnswer2 = 'd';
  65. $questions[] = createQuestions($text2, $points2, $answers2, $correctAnswer2);
  66. var_dump($questions);
  67. printQuestions($questions);
  68.  
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
array(1) {
  [0]=>
  object(Question)#1 (4) {
    ["text"]=>
    string(106) "Какая планета располагается четвертой по счету от Солнца?"
    ["points"]=>
    int(10)
    ["answers"]=>
    array(4) {
      ["a"]=>
      string(12) "Венера"
      ["b"]=>
      string(8) "Марс"
      ["c"]=>
      string(12) "Юпитер"
      ["d"]=>
      string(16) "Меркурий"
    }
    ["correctAnswer"]=>
    string(1) "b"
  }
}
array(2) {
  [0]=>
  object(Question)#1 (4) {
    ["text"]=>
    string(106) "Какая планета располагается четвертой по счету от Солнца?"
    ["points"]=>
    int(10)
    ["answers"]=>
    array(4) {
      ["a"]=>
      string(12) "Венера"
      ["b"]=>
      string(8) "Марс"
      ["c"]=>
      string(12) "Юпитер"
      ["d"]=>
      string(16) "Меркурий"
    }
    ["correctAnswer"]=>
    string(1) "b"
  }
  [1]=>
  object(Question)#2 (4) {
    ["text"]=>
    string(85) "Какой город является столицей Великобритании?"
    ["points"]=>
    int(6)
    ["answers"]=>
    array(4) {
      ["a"]=>
      string(10) "Париж"
      ["b"]=>
      string(12) "Москва"
      ["c"]=>
      string(15) "Нью-Йорк"
      ["d"]=>
      string(12) "Лондон"
    }
    ["correctAnswer"]=>
    string(1) "d"
  }
}
1. Какая планета располагается четвертой по счету от Солнца?

Варианты ответов:
  a. Венера
  b. Марс
  c. Юпитер
  d. Меркурий
2. Какой город является столицей Великобритании?

Варианты ответов:
  a. Париж
  b. Москва
  c. Нью-Йорк
  d. Лондон