在 PHP 中,可以嵌套使用 include
或 require
語句。這意味著你可以在一個 include
或 require
文件中包含另一個文件。這種方法有助于將代碼拆分成多個可重用的組件。
例如,假設你有以下目錄結構:
- index.php
- header.php
- footer.php
你可以在 index.php
文件中使用 include
語句來包含 header.php
和 footer.php
文件:
<?php
// index.php
include 'header.php';
// Your main content here
include 'footer.php';
?>
在這個例子中,header.php
和 footer.php
文件中的內容將被包含在 index.php
文件中。這種方法使得代碼更加模塊化和易于維護。