[英]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.