[英]Dynamic linking in PHP
我正在創建一個用於學習的自定義 CMS。 我計划有以下幾頁,
我很困惑索引頁如何鏈接頁眉、頁腳和側邊欄。 請指導我如何在 index.php 中鏈接這些 php 文件。
您可以簡單地添加要包含的文件數組:
$array = ('header.php', 'footer.php', 'sidebar.php');
然后添加一些 HTML 代碼結構...
然后您可以訪問數組並加載文件。
include_once($array[0]);
.. 包含 header.php
include_once($array[1]);
.. 包含footer.php
....
如果文件不存在,您可以使用 require_once 函數使您的站點不加載其他內容。
如果你想自動添加這些文件,只需添加一個循環。
foreach($array as $file){
if(file_exists($file)){
require_once($file);
}
else{
die($file.' does not exist!');
}
}
您可以使用require_once
或include
。 我個人使用 require once 選項,因為它只需要包含一次,而不會在后期再次包含。
在你的body
標簽:
require_once('/path/to/header.php');
require_once('/path/to/breadcrumb.php');
require_once('/path/to/content.php');
require_once('/path/to/footer.php');
require_once('/path/to/copyright.php');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.