PHP require_once 语句
简介
PHP 中的**require_once** 语句在功能上类似于 require 语句。唯一的区别是,如果某个文件已经包含在处理中,则不会再次包含该文件。请注意,即使使用了 require_once 语句,使用 include 或 require 语句包含的文件也不会再次包含。
require_once 语句的其他行为类似于 require 语句。
require_once 示例
在以下示例中,主 php 脚本包含 test.php
示例
<?php echo "inside main script
"; echo "now including with require_once test.php script
"; require_once "test.php"; echo "try to include it again"; require_once "test.php"; ?> //test.php <?php echo "File included
"; ?>
输出
当从命令行运行主脚本时,将产生以下结果 −
inside main script now including with require_once test.php script File included try to include it again
failed require_once 的错误
在尝试添加不存在的文件时,require_once 语句也会导致致命错误
示例
<?php echo "inside main script
"; echo "now including with require_once notest.php script
"; require_once "notest.php"; ?>
输出
将产生以下结果。请注意,程序在发生错误时已终止 −
inside main script now including with require_once notest.php script PHP Fatal error: require_once(): Failed opening required 'notest.php' (include_path='C:\xampp\php\PEAR') in line 4 Fatal error: require_once(): Failed opening required 'notest.php' (include_path='C:\xampp\php\PEAR') in line 4
广告