QuizAPP
QuizAPP
<?php
session_start();
$questions = [
1 => [
'question' => 'What is the capital of France?',
'options' => [
'A' => 'London',
'B' => 'Berlin',
'C' => 'Paris',
'D' => 'Madrid'
],
'correct' => 'C'
],
2 => [
'question' => 'Which planet is known as the Red Planet?',
'options' => [
'A' => 'Venus',
'B' => 'Mars',
'C' => 'Jupiter',
'D' => 'Saturn'
],
'correct' => 'B'
],
3 => [
'question' => 'What is 2 + 2?',
'options' => [
'A' => '3',
'B' => '4',
'C' => '5',
'D' => '22'
],
'correct' => 'B'
],
4 => [
'question' => 'Who wrote "Romeo and Juliet"?',
'options' => [
'A' => 'Charles Dickens',
'B' => 'Jane Austen',
'C' => 'William Shakespeare',
'D' => 'Mark Twain'
],
'correct' => 'C'
],
5 => [
'question' => 'Which element has the chemical symbol "O"?',
'options' => [
'A' => 'Gold',
'B' => 'Oxygen',
'C' => 'Osmium',
'D' => 'Oganesson'
],
'correct' => 'B'
]
];
// Check if correct
if ($selected_answer == $questions[$current]['correct']) {
$_SESSION['score']++;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP Quiz App</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 600px;
margin: 0 auto;
padding: 20px;
}
.quiz-container {
background-color: #f5f5f5;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.question {
font-size: 18px;
margin-bottom: 20px;
}
.options {
margin-bottom: 20px;
}
.option {
margin-bottom: 10px;
}
.result {
margin-top: 20px;
font-weight: bold;
}
.correct {
color: green;
}
.incorrect {
color: red;
}
button {
background-color: #4CAF50;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="quiz-container">
<h1>PHP Quiz App</h1>
<div class="options">
<?php foreach ($question['options'] as $key => $option): ?>
<div class="option">
<input type="radio" id="option<?php echo $key; ?>"
name="answer" value="<?php echo $key; ?>" required>
<label for="option<?php echo $key; ?>"><?php echo $key; ?>.
<?php echo $option; ?></label>
</div>
<?php endforeach; ?>
</div>
<h3>Your Answers:</h3>
<?php foreach ($_SESSION['answers'] as $q_num => $answer): ?>
<div class="<?php echo ($answer == $questions[$q_num]['correct']) ?
'correct' : 'incorrect'; ?>">
<p>
Question <?php echo $q_num; ?>:
You selected <?php echo $answer; ?> (<?php echo
$questions[$q_num]['options'][$answer]; ?>).
<?php if ($answer != $questions[$q_num]['correct']): ?>
The correct answer was <?php echo $questions[$q_num]['correct'];
?>
(<?php echo $questions[$q_num]['options'][$questions[$q_num]
['correct']]; ?>).
<?php endif; ?>
</p>
</div>
<?php endforeach; ?>