如何使用PHP显示一个或另一个html页面?

如何使用PHP显示一个或另一个html页面?,php,html,if-statement,Php,Html,If Statement,我想使用PHP和if语句,我想这样做 if ($variable){ display html page1 } else { display html page2 } 我该怎么做?请注意,我不想将用户重定向到其他页面 --编辑-- 我对其中一个这样做没有问题,但对另一个文件来说,这样做太麻烦了 --编辑-- 以下是迄今为止的编码: <?PHP include 'uc.php'; if ($UCdisplay) { include("under_construction.php")

我想使用PHP和if语句,我想这样做

if ($variable){
display html page1
}
else {
display html page2
}
我该怎么做?请注意,我不想将用户重定向到其他页面

--编辑-- 我对其中一个这样做没有问题,但对另一个文件来说,这样做太麻烦了

--编辑-- 以下是迄今为止的编码:

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


我的问题是,如果我必须为每个php页面创建一个html页面,这将非常复杂和混乱,因此我需要某种方法来显示整个html页面,而不是使用include(“index.html”)

最简单的方法是将html放在两个单独的文件中,并使用:


使用三元运算符:

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

如果您希望避免创建“每个php页面的html页面”,那么您可以这样做,将“真实”内容直接放在php页面内

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

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

</html>

其思想是:如果
$UCdisplay
为真,则显示正在构建的页面,执行在
退出时停止-未显示任何其他内容。否则,程序流“通过”并输出页面的其余部分。每一页内容都有一个PHP文件

您可以通过将检查
$UCdisplay
的代码直接移动到uc.php中来回避这个问题;这将避免您必须在每个文件的顶部编写相同的if语句。诀窍是在包含构建页面后让代码退出。

对于仍在查看的用户: 请参阅
readfile()函数。它在一个函数中读取和打印文件

定义

int readfile ( string $filename [, bool $use_include_path = false [, resource $context ]] )
读取文件并将其写入输出缓冲区

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

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

</html>
int readfile ( string $filename [, bool $use_include_path = false [, resource $context ]] )