PHP include
语句
最后修改于 2025 年 4 月 16 日
PHP 的 include
语句允许在执行前将一个 PHP 文件的内容插入到另一个文件中。这实现了代码重用和模块化编程。包含的文件与包含文件共享相同的变量作用域。
基本定义
include
语句会包含并评估指定的文件。如果找不到文件,PHP 会发出警告但继续执行。
相关的语句是 require
、include_once
和 require_once
。Require 在文件丢失时会停止执行。
*_once
变体可防止对同一文件的多次包含。这可以避免重复包含导致的函数重定义和变量重新赋值。
基本文件包含
此示例演示了在主页面中包含一个简单的页眉文件。
<?php declare(strict_types=1); include 'header.php'; echo "<main>Welcome to our website!</main>"; include 'footer.php';
<!DOCTYPE html> <html> <head> <title>My Website</title> </head> <body> <header> <h1>Website Header</h1> </header>
主文件包含页眉和页脚文件。包含的文件成为最终输出的一部分。这允许跨页面实现一致的页眉/页脚。路径可以是相对的或绝对的。
包含配置文件
此示例展示了包含带有设置和常量的配置文件。
<?php declare(strict_types=1); const DB_HOST = 'localhost'; const DB_NAME = 'mydb'; const DB_USER = 'admin'; const DB_PASS = 'secret';
<?php declare(strict_types=1); include 'config.php'; $connection = new PDO( "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASS ); echo "Connected to database: " . DB_NAME;
配置文件定义了数据库常量。主应用程序文件包含它以访问这些设置。这集中了配置管理。更改只需要在一个文件中进行。
包含函数库
此示例演示了包含包含可重用函数的文件的过程。
<?php declare(strict_types=1); function formatDate(string $date): string { return date('F j, Y', strtotime($date)); } function sanitizeInput(string $input): string { return htmlspecialchars(trim($input)); }
<?php declare(strict_types=1); include 'functions.php'; $userInput = " <script>alert('xss')</script> "; $cleanInput = sanitizeInput($userInput); $joinDate = '2023-04-15'; echo "Member since: " . formatDate($joinDate);
函数文件包含实用函数。个人资料页面包含它以使用这些函数。这促进了跨多个页面的代码重用。函数对包含文件可用。
条件包含
此示例展示了如何根据特定条件包含文件。
<?php declare(strict_types=1); $userRole = 'admin'; if ($userRole === 'admin') { include 'admin-menu.php'; } else { include 'user-menu.php'; } echo "<div class='content'>Main page content</div>";
<nav> <a href='dashboard.php'>Dashboard</a> <a href='users.php'>Manage Users</a> <a href='settings.php'>Settings</a> </nav>
页面根据用户角色包含不同的菜单文件。这允许动态内容包含。条件决定包含哪个文件。所有变量对包含的文件都可用。
包含路径配置
此示例演示了设置包含路径和使用绝对路径。
<?php declare(strict_types=1); // Add directory to include path set_include_path(get_include_path() . PATH_SEPARATOR . '/var/www/includes'); // Now can include files from that directory include 'utilities.php'; // Or use absolute path include __DIR__ . '/templates/header.php'; echo "Application initialized";
该脚本配置了其他包含路径。然后可以从这些路径包含文件,而无需完整的路径。__DIR__
提供当前目录。这使得跨环境的包含更具可移植性。
Include 与 Require 的区别
此示例比较了 include 和 require 语句及其行为。
<?php declare(strict_types=1); // Will emit warning but continue execution include 'nonexistent.php'; echo "This still executes after include failure"; // Will halt execution with fatal error require 'nonexistent.php'; echo "This line won't be reached";
Include 会为丢失的文件发出警告但继续执行。Require 会因丢失文件而停止执行。根据文件的重要性进行选择。关键文件应使用 require。可选文件可以使用 include。
Include_once 示例
此示例演示了使用 include_once 防止多次包含。
<?php declare(strict_types=1); function initializeApp() { echo "Application initialized\n"; } initializeApp();
<?php declare(strict_types=1); include_once 'init.php'; include_once 'init.php'; // Won't be included again echo "Main application code";
init.php 文件包含初始化代码。Main.php 使用 include_once 包含它两次。第二次包含被跳过。这可以防止函数重定义错误。对于应该只加载一次的文件很有用。
最佳实践
- 安全: 验证文件路径以防止目录遍历。
- 组织:将包含的文件放在专用目录中。
- 性能:对于经常包含的文件,请使用 opcache。
- 错误处理:在包含关键文件之前检查其是否存在。
- 命名:为包含的文件使用清晰的命名约定。
来源
本教程涵盖了 PHP 文件包含,并通过各种场景的 include 用法和最佳实践的实际示例进行了说明。
作者
列出 所有 PHP 教程。