views:

60

answers:

4

Hi! I wanted to use PHP and the if statement, and I wanted to do

if ($variable){
display html page1
}
else {
display html page2
}

How do I do this? An please note, that I do not want to redirect the user to a different page.

--EDIT-- I would have no problem doing that with one of them, but the other file, it would be too much of a hassle to do that.

--EDIT-- Here is the coding so far:

<?PHP
include 'uc.php';
if ($UCdisplay) {
    include("under_construction.php");
}
else {
    include("index.html");
}
?>

My problem is that it would be really complicated and confusing if I were to have to create an html page for every php page, so I need some way to show the full html page instead of using include("index.html")

+4  A: 
if ($variable){
  include("file1.html");
}
else {
  include("file2.html");
}
Thomas
+2  A: 

The easiest way would be to have your HTML in two separate files and use include():

if ($variable) {
    include('page1.html');
}
else {
    include('page2.html');
}
zombat
+2  A: 

using the ternary operator:

 include(($variable ? 'page1' : 'page2').'.html');
Alex LE
A: 

If you want to avoid creating "an html page for every php page", then you could do something like this, with the "real" content directly inside the PHP page.

<?PHP
include 'uc.php';
if ($UCdisplay) {
    include("under_construction.php");
    exit;
}
?>
<html>

<!-- Your real content goes here -->

</html>

The idea is this: If $UCdisplay is true, then your under construction page is shown, and execution stops at exit; - nothing else is shown. Otherwise, program flow "falls through" and the rest of the page is output. You'll have one PHP file for each page of content.

You could side-step this issue by moving the code that checks $UCdisplay directly into uc.php; this would prevent you from having to write that same if statement at the top of every file. The trick is to have the code exit after you include the construction page.

meagar