php echo include

时间:2009-09-22 21:03:05

标签: php wsod

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

我不知道为什么,但每当我在我的剧本中添加该行时,我都会看到空白屏幕:(

10 个答案:

答案 0 :(得分:7)

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

Include不会返回解析文件的结果,所以在这里连接是没有意义的。

答案 1 :(得分:4)

运行这段代码时:

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是否符合预期......


实际上,include manual page上的示例#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");

效果更好:没有警告 - 文件实际上已正确包含。<​​/ p>


作为旁注:如果你得到一个白色屏幕,可能是因为你的配置“隐藏”了错误和警告 - 这对于开发环境来说并不是很好:显示那些会让你停顿不前。

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

或者,如果您无法访问该文件,则可以使用error_reportingini_set - 在您的脚本开头可能会执行以下操作:

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

注意:当然,您不希望在生产环境中记录错误(请参阅log_errors),而不显示错误。

答案 2 :(得分:1)

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

答案 3 :(得分:1)

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 () or die() not working

答案 4 :(得分:1)

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

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

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

“include”指令不返回任何内容,因此尝试将其附加到字符串不起作用。

答案 5 :(得分:0)

输出file.php的内容,你可能想要使用http://us3.php.net/file_get_contents

答案 6 :(得分:0)

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

答案 7 :(得分:0)

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

答案 8 :(得分:0)

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

Reference Manual表明,如果有的话,它会返回一个布尔值。

答案 9 :(得分:-1)

file.php末尾是否有额外的换行符?