Include, include_once, require and require_once are all very similar. Basically using anyone of them inserts the php of the file that is being "included" just like if you had all the php from that file in the if/else.
So basically make a php file, it can have almost any PHP, HTML, Javascript or CSS in it and then "include" it in the if/else and see what happens.
If this is the correctanswer.php
<?php
$var ="hello world";
?>
<html>
<head>
<title>Correct Answer Page</title>
</head>
<body>
<div style="width: 500px; font-size: 30px; text-align: center;"><?php echo $var; ?></div>
</body>
</html>
And this is the wronganswer.php
<?php
$var ="Wrong Answer";
?>
<html>
<head>
<title>Wrong Answer Page</title>
</head>
<body>
<div style="width: 500px; font-size: 30px; text-align: center;"><?php echo $var; ?></div>
</body>
</html>
And this is the include
<?php
if ((isset($_POST['chalQ'])) && ($_POST['chalQ'] == $trueanswer)) {
include 'correctanswer.php';
}
else {
include 'wronganswer.php';
}
?>
All that is the same as doing this
<?php
if ((isset($_POST['chalQ'])) && ($_POST['chalQ'] == $trueanswer)) {
$var ="hello world";
?>
<html>
<head>
<title>Correct Answer Page</title>
</head>
<body>
<div style="width: 500px; font-size: 30px; text-align: center;"><?php echo $var; ?></div>
</body>
</html>
<?php
}
else {
$var ="Wrong Answer";
?>
<html>
<head>
<title>Wrong Answer Page</title>
</head>
<body>
<div style="width: 500px; font-size: 30px; text-align: center;"><?php echo $var; ?></div>
</body>
</html>
<?php
}
?>
I have build sites that have had 20 or more includes in a single page load and many times there is an include inside an include that is in another include. Or I will have one of the nested includes refer to a function that was in a totally different included file.
Includes just allow you to break up your code so that you don't end up with a file that is 10,000+ lines long which can make it difficult to find where different parts of the code were placed in the file.