2014-02-06 43 views
2

使用PHP文件的正確方法是什麼? includes.php爲不同的事情,然後將這些部分拉入其他PHP文件?使用中央PHP包含文件

例如,在includes.php

#footer <p>&copy; Copyright 2014 | <a href="legal.php">Legal</a></p>

然後在index.php

<?php include("includes.php#footer"); ?>

是這樣的事情可能嗎?

+0

你可以在'includes.php'創建一個函數你從'index.php'中引用的內容 – Joren

+0

沒有「正確」的模式,但我個人會避免像你所描述的那樣。我會將每個部分分離到它自己的文件中並明確包含它們。例如:'include(「partials/header.php」);','include(「partials/footer.php」);' –

回答

0

你可以嘗試存儲陣列,您includes.php

return array(
    'footer' => '<p>&copy; Copyright 2014 | <a href="legal.php">Legal</a></p>', 
); 

,比index.php把它以這樣一種方式:

$templates = include("includes.php#footer"); 
$footer = $templates["footer"];