在 PHP 中,您可以使用 include
或 require
语句来引入其他 PHP 文件。这两个语句的主要区别在于,如果引入的文件不存在或无法读取,require
会产生一个致命错误(fatal error),而 include
只会产生一个警告(warning),但脚本会继续执行。
以下是使用 include
和 require
的示例:
include
引入 PHP 文件:<?php
// 当前文件路径:/path/to/your/file.php
include 'other_file.php'; // 引入 other_file.php
echo "This is the main file.";
?>
require
引入 PHP 文件:<?php
// 当前文件路径:/path/to/your/file.php
require 'other_file.php'; // 引入 other_file.php
echo "This is the main file.";
?>
在这两个示例中,other_file.php
将被引入到当前文件中,并且其内容将在当前文件的相应位置执行。