gpt4 book ai didi

PHP require 失败且没有错误

转载 作者:可可西里 更新时间:2023-11-01 13:43:16 29 4
gpt4 key购买 nike

这是我苦苦思索了几个小时的代码:

if ((require $_SESSION['ROOT_PATH'] . '/templates/core/menu_js.php') == 'OK') {
echo 'OK';
} else {
echo 'KO';
}

如果我正确理解有关“require”指令的 PHP 文档,则永远不应编写“KO”,因为如果 require 不起作用,则会引发错误。

在我的例子中,“KO”总是显示,即使有错误调整:

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_COMPILE_ERROR);

请注意,相同的要求(或包含)在网站的其他页面中也能完美运行。

编辑

看了文档后添加了 if 结构。起初,我只有一行:

require $_SESSION['ROOT_PATH'] . '/templates/core/menu_js.php';

当我检查这条线没有工作时,我添加了 if

顺便说一下。必需页面(当它工作时)将 script 标记添加到我从未在此独特页面上看到的调用页面。在使用此要求的任何其他页面上,脚本会出现在输出中。

所以我的问题应该是“如果没有显示所需 php 文件的输出,为什么没有出现错误?”

最佳答案

根据 the documentation

Successful includes, unless overridden by the included file, return 1... Also, it's possible to return values from included files. You can take the value of the include call as you would for a normal function.

所以您的文件没有返回“OK”。它返回 1(表示成功)或自定义值。

require 是一种语言构造,而不是标准函数。通过使用 require,您已经表明如果找不到文件,代码应该失败。您很可能不需要检查返回值。

关于PHP require 失败且没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12955837/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com