How can PHP beginners effectively manage and display multiple questions and answers in a survey application?
One way to effectively manage and display multiple questions and answers in a survey application is to use arrays to store the questions and answers. You can create multidimensional arrays where each question is associated with its corresponding answers. Then, you can loop through the arrays to display the questions and provide input fields for the answers.
<?php
// Define an array of questions with their corresponding answers
$survey = array(
array(
'question' => 'What is your favorite color?',
'answers' => array('Red', 'Blue', 'Green', 'Yellow')
),
array(
'question' => 'How often do you exercise?',
'answers' => array('Everyday', '3-4 times a week', '1-2 times a week', 'Rarely')
)
);
// Loop through the survey array to display questions and input fields for answers
foreach ($survey as $question) {
echo '<p>' . $question['question'] . '</p>';
echo '<select name="' . str_replace(' ', '_', $question['question']) . '">';
foreach ($question['answers'] as $answer) {
echo '<option value="' . $answer . '">' . $answer . '</option>';
}
echo '</select><br>';
}
?>