何时在PHP中包含两次脚本?

Dat*_*ame 6 php include

我知道include_once并且require_once只允许被调用的脚本运行一次.我的问题是:如果我不使用_once,它会一直运行多次吗?默认情况下它会运行多次吗?我搜索了答案,但没有找到答案.

Opt*_*ime 7

include每次写时都会运行一次.使用的关键include_once是你可以做到

include "myfile.php"; // Include file one time
include "myfile.php"; // Include file again
Run Code Online (Sandbox Code Playgroud)

这是完全合法的,在某些情况下你会想要这样做.

问题是,如果您在此文件中定义一个函数,php会抱怨您不能多次定义该方法(因为该名称已被采用).

include_once 确保同一个文件不会被包含多次,所以如果你这样做:

include_once "myfile.php"; // Include the file one time
include_once "myfile.php"; // This is ignored
Run Code Online (Sandbox Code Playgroud)

...只执行一个包含.