[英]Dynamic linking in PHP
I am creating a custom CMS for learning.我正在创建一个用于学习的自定义 CMS。 I have the plan to have the following pages,
我计划有以下几页,
I am confused how would the index page link header, footer and sidebar.我很困惑索引页如何链接页眉、页脚和侧边栏。 Please guide me how can I link these php files in index.php.
请指导我如何在 index.php 中链接这些 php 文件。
You can simply add an Array of files you want to include:您可以简单地添加要包含的文件数组:
$array = ('header.php', 'footer.php', 'sidebar.php');
Then add some HTML Code structure...然后添加一些 HTML 代码结构...
and then you can access the Array and load files.然后您可以访问数组并加载文件。
include_once($array[0]);
.. to include the header.php .. 包含 header.php
include_once($array[1]);
.. to include the footer.php .. 包含footer.php
.... ....
you can use the require_once function to make your site not loading other content if the file does not exists.如果文件不存在,您可以使用 require_once 函数使您的站点不加载其他内容。
if you want to add these files automaticly just add a loop.如果你想自动添加这些文件,只需添加一个循环。
foreach($array as $file){
if(file_exists($file)){
require_once($file);
}
else{
die($file.' does not exist!');
}
}
You can use either require_once
or include
.您可以使用
require_once
或include
。 I personally use require once option cause it only needs to be include once and not again on later stage.我个人使用 require once 选项,因为它只需要包含一次,而不会在后期再次包含。
Inside your body
tags:在你的
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.