php 回显包含

发布于 2024-08-05 23:27:40 字数 158 浏览 2 评论 0原文

echo ("<div style=\"text-align:center\">" . include ('file.php') . "</div>\n");

我不知道为什么,但每当我将该行放入脚本中时,我都会得到空白屏幕:(

echo ("<div style=\"text-align:center\">" . include ('file.php') . "</div>\n");

I'm not sure why, but whenever I put that line in my script I get blank screen :(

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

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

发布评论

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

评论(10

孤城病女 2024-08-12 23:27:40
echo "<div style=\"text-align:center\">";
include ('file.php');
echo "</div>\n";

Include 不会返回文件解析的结果,因此在此处连接没有任何意义。

echo "<div style=\"text-align:center\">";
include ('file.php');
echo "</div>\n";

Include does not return the result of the parsing of the file so it makes no sense whatsoever to concatenate here.

静若繁花 2024-08-12 23:27:40

运行这段代码时:

echo ("<div style=\"text-align:center\">" . include ('temp-2.php') . "</div>\n");

(我从您的示例中复制粘贴,仅更改文件名)

我收到此警告:

Warning: include(temp-2.php</div> ) [function.include]: failed to open stream: No such file or directory

不确定 ()include 指令正在执行您期望的操作...

实际上,包含手册页示例#4 > 看起来可以解释这一点:

<?php
// won't work, evaluated as include(('vars.php') == 'OK'), i.e. include('')
if (include('vars.php') == 'OK') {
    echo 'OK';
}

// works
if ((include 'vars.php') == 'OK') {
    echo 'OK';
}
?>

事实上,将您的代码重写为:

echo ("<div style=\"text-align:center\">" . (include 'temp-2.php') . "</div>\n");

效果更好:没有警告 - 并且该文件实际上已正确包含。

作为旁注:如果您遇到白屏,可能是因为您的配置“隐藏”了错误和警告——这对于开发环境来说不太好:显示这些内容会让您陷入相当大的困难。

要更改此设置,您可以使用 error_reportingdisplay_errors php.ini 文件中的选项。

或者,如果您无权访问该文件,则可以使用 error_reportingini_set ——类似这样在脚本的开头可能会这样做:

error_reporting(E_ALL);
ini_set('display_errors', 'On');

注意:当然,您不希望在生产环境中这样做,您应该在生产环境中记录错误(请参阅 log_errors),并且不显示它们。

When running this piece of code :

echo ("<div style=\"text-align:center\">" . include ('temp-2.php') . "</div>\n");

(Which I copy-pasted from your example, only changing the name of the file)

I get this warning :

Warning: include(temp-2.php</div> ) [function.include]: failed to open stream: No such file or directory

Not sure the () for the include directive are doing what you expect...

Actually, example #4 on the include manual page looks like it might explain this :

<?php
// won't work, evaluated as include(('vars.php') == 'OK'), i.e. include('')
if (include('vars.php') == 'OK') {
    echo 'OK';
}

// works
if ((include 'vars.php') == 'OK') {
    echo 'OK';
}
?>

And, indeed, rewriting your code to this :

echo ("<div style=\"text-align:center\">" . (include 'temp-2.php') . "</div>\n");

Works much better : no warning -- and the file is actually included correctly.

As a sidenote : if you are getting a white screen, it's probably because your configuration "hides" errors and warnings -- which is not quite nice for a development environnement : having those displayed would halp you quite a bit.

To change that, you can use the error_reporting and display_errors options in your php.ini file.

Or, if you don't have access to that one, you can use error_reporting and ini_set -- something like this at the beginning of your script might do :

error_reporting(E_ALL);
ini_set('display_errors', 'On');

Note : of course, you don't want that on your production environment, on which you should log errors (see log_errors), and not display them.

关于从前 2024-08-12 23:27:40

我不知道你是否可以做这样的事情。
首先执行包含,然后执行包含文件中任何给定变量的回显。

I dont know if you can do such a thing.
Do the include first and then do the echo of any given variable from the include file.

jJeQQOZ5 2024-08-12 23:27:40

我认为你需要做这样的事情:

ob_start();
include('file.php');
$contents = ob_get_clean();

echo ("<div style=\"text-align:center\">" . $contents . "</div>\n");

“include”指令不会返回任何内容,因此尝试将其附加到字符串中是行不通的。

I think you would need to do something like this:

ob_start();
include('file.php');
$contents = ob_get_clean();

echo ("<div style=\"text-align:center\">" . $contents . "</div>\n");

the "include" directive doesn't return anything, so trying to append it to a string is not going to work.

別甾虛僞 2024-08-12 23:27:40

include 不是一个函数,而是一种特殊的语言构造。这也是为什么您不需要在“参数”两边加上括号的原因,因为 include 只有一个“参数”。将其括在括号中就像将任何其他值括在括号中一样:

1 === (1) === ((1)) === (((1))) === …

相反,您需要将整个构造括在括号中:

echo "<div style=\"text-align:center\">" . (include 'file.php') . "</div>\n";

但是由于 include 不会返回所包含脚本文件的输出,而是直接打印它,因此您可以需要缓冲输出并将其返回到包含该文件的脚本:

<?php // file.php
    ob_start();
    // …
    return ob_get_clean();

这可以工作。但我认为你不想这样做。所以这可能更容易:

echo "<div style=\"text-align:center\">";
include 'file.php';
echo "</div>\n";

顺便说一句:echoprintexitdie是特殊的语言结构也。另请参阅require_once () 或 die() 不起作用

include is not a function but a special language construct. That’s also the reason why you don’t need the parenthesis around the “parameter” as include only has one “parameter”. Wrapping that in parenthesis is like wrapping any other value in parenthesis:

1 === (1) === ((1)) === (((1))) === …

Instead you need to wrap the whole construct in parenthesis:

echo "<div style=\"text-align:center\">" . (include 'file.php') . "</div>\n";

But since include does not return the output of the included script file but prints it directly, you would need to buffer the output and return it to the script that included that file with:

<?php // file.php
    ob_start();
    // …
    return ob_get_clean();

That would work. But I don’t think that you want to do that. So this is probably quite easier:

echo "<div style=\"text-align:center\">";
include 'file.php';
echo "</div>\n";

By the way: echo, print, exit and die are special language constructs too. See also require_once () or die() not working

海拔太高太耀眼 2024-08-12 23:27:40

要输出 file.php 的内容,您可能需要使用 https://www.php.net/file_get_contents

to output the contents of file.php you'll probably want to use https://www.php.net/file_get_contents

挽袖吟 2024-08-12 23:27:40

file.php 中有什么内容吗?它可能是空的,或者其中的某些内容可能导致错误。

Is there anything in file.php? It may be empty, or something in it may be causing an error.

时光暖心i 2024-08-12 23:27:40

在浏览器中查看源代码,而不仅仅是可见部分。通常您可以通过按 Ctrl+U 来完成此操作。如果它只是输出一个空的 div 元素,您将在浏览器中看到一个空白屏幕。请记住,.php 文件很可能在输出之前在您的设置中进行解析,这意味着您永远不会看到 标记以及它们之间的代码。

Look at the source code in your browser, not just the visible part. Usually you can do this by pressing Ctrl+U. If it was just outputting an empty div element, you would see a blank screen in the browser. And keep in mind that .php files are most likely parsed in your setup before being output, which means you wouldn't ever see <?php ?> tags and the code inbetween them.

如梦亦如幻 2024-08-12 23:27:40

我从未想过将 include 作为一个返回可以打印的字符串的函数。

参考手册建议,如果有的话,它返回一个布尔值。

I never thought of include as a function which returns a string you could print.

The Reference Manual suggests that, if anything, it returns a boolean value.

因为看清所以看轻 2024-08-12 23:27:40

file.php 末尾是否有多余的换行符?

Is there an extra line break at the end of file.php?

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