PHP 检查 require_once() 内容是否为空

发布于 2024-10-30 19:05:39 字数 347 浏览 2 评论 0 原文

我正在编写一个 PHP 脚本,其中涉及通过“require_once()”方法包含几个外部 PHP 脚本。我想知道是否有一种方法可以让主脚本(包括其他脚本)判断包含脚本的已处理输出是否生成了任何内容。

例如,用户对特定脚本的权限可能会导致 PHP 不生成任何输出。因此,也许主脚本会回显类似的内容:

                             Nothing interesting here!

有没有办法在主脚本中执行此操作,或者我需要在包含的脚本内创建这些测试,并将结果返回到主脚本?

谢谢您的宝贵时间,
斯普里诺724

I am working on a PHP script which involves me including several external PHP scripts via the "require_once()" method. I would like to know if there is a way for the master script (the one including the others) to tell whether or not the processed output from included script has generated any content.

So, for example, maybe the user's permissions for a particular script results in PHP not generating any output. So, maybe, the master script would echo something like:

                             Nothing interesting here!

Is there a way to do that in the master script, or would I need to create these tests inside of the included script, and return the results to the master script?

Thank you for your time,
spryno724

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

下雨或天晴 2024-11-06 19:05:39

您可以使用 ob_startob_get_contentsob_end_clean 像这样:

ob_start();
require_once('script.php');
$output = ob_get_contents();
ob_end_clean();

You can capture the output using ob_start, ob_get_contents and ob_end_clean like this:

ob_start();
require_once('script.php');
$output = ob_get_contents();
ob_end_clean();
我做我的改变 2024-11-06 19:05:39
ob_start();
require_once 'your_file.php';
$output = ob_get_flush(); // ob_get_clean() if you want to suppress the output

if(empty($output)) {
    echo 'Nothing interesting here!';
}
ob_start();
require_once 'your_file.php';
$output = ob_get_flush(); // ob_get_clean() if you want to suppress the output

if(empty($output)) {
    echo 'Nothing interesting here!';
}
攀登最高峰 2024-11-06 19:05:39

如果 require_once 没有被执行,不用担心。尝试使用如下调试功能来确定 require_once 文件的变量/函数是否正常工作:-

var_dump($Your_Variable_Name_from_require_Once_file);

Do not worry if the require_once is executed on not. Try to identify if the variables/functions of require_once files are working or not by using the debug function like this:-

var_dump($Your_Variable_Name_from_require_Once_file);
孤单情人 2024-11-06 19:05:39

如果您愿意,也可以使用:

@$result = include $filename;

当找不到文件时,include 确实返回 false,但它也会生成警告。这就是为什么你需要@。

我很想使用 file_exists() 但是正如 php 文档中的评论所述, file_exists() 不会搜索 PHP INCLUDE_PATH 并且该函数的结果被缓存。

因此,如果您决定使用 file_exists() 那么您可能会想要使用clearstatcache(),在这种情况下您可能想看看这里:

https://www.php.net/manual/en/function.clearstatcache.php#125163

和此处:

clearstatcache + include_path + 会话

If you like you can also use:

@$result = include $filename;

include does return false when the file can't be found, but it does also generate a warning. That's why you need the @.

I was tempted to use file_exists() however as noted in a comment in php docs, file_exists() DOESN'T SEARCH THE PHP INCLUDE_PATH and the results of this function are cached.

So if you decided to use file_exists() then you will want to probably use clearstatcache() that being the case you may want to have a look here:

https://www.php.net/manual/en/function.clearstatcache.php#125163

and here :

clearstatcache + include_path + sessions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文