繁体   English   中英

如何使用PHP显示一个或多个HTML页面?

[英]How do I use PHP to display one html page or another?

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

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

我该怎么做呢? 请注意,我不想将用户重定向到其他页面。

--EDIT--我对其中一个做这个没有问题,但另一个文件,这样做太麻烦了。

--EDIT--到目前为止这是编码:

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

我的问题是,如果我必须为每个php页面创建一个html页面,那将是非常复杂和令人困惑的,所以我需要一些方法来显示完整的html页面而不是使用include(“index.html”)

if ($variable){
  include("file1.html");
}
else {
  include("file2.html");
}

最简单的方法是将HTML放在两个单独的文件中并使用include()

if ($variable) {
    include('page1.html');
}
else {
    include('page2.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为真,那么就显示你的构建页面,并且exit;执行停止exit; - 没有显示任何其他内容。 否则,程序流“通过”并输出页面的其余部分。 每页内容都有一个PHP文件。

您可以通过将检查$UCdisplay的代码直接移动到uc.php中来解决此问题。 这将阻止您必须在每个文件的顶部写入相同的if语句。 诀窍是在包含构造页面后exit代码。

对于那些仍在寻找的人:请参阅readfile(); 在PHP中的功能。 它在一个函数中读取并打印文件。

定义

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

读取文件并将其写入输出缓冲区。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM