php 回显包含
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
Include 不会返回文件解析的结果,因此在此处连接没有任何意义。
Include does not return the result of the parsing of the file so it makes no sense whatsoever to concatenate here.
运行这段代码时:
(我从您的示例中复制粘贴,仅更改文件名)
我收到此警告:
不确定
()
的include
指令正在执行您期望的操作...实际上,包含手册页示例#4 > 看起来可以解释这一点:
事实上,将您的代码重写为:
效果更好:没有警告 - 并且该文件实际上已正确包含。
作为旁注:如果您遇到白屏,可能是因为您的配置“隐藏”了错误和警告——这对于开发环境来说不太好:显示这些内容会让您陷入相当大的困难。
要更改此设置,您可以使用
error_reporting
和
display_errors
php.ini 文件中的选项。或者,如果您无权访问该文件,则可以使用
error_reporting
和ini_set
——类似这样在脚本的开头可能会这样做:注意:当然,您不希望在生产环境中这样做,您应该在生产环境中记录错误(请参阅
log_errors
),并且不显示它们。When running this piece of code :
(Which I copy-pasted from your example, only changing the name of the file)
I get this warning :
Not sure the
()
for theinclude
directive are doing what you expect...Actually, example #4 on the include manual page looks like it might explain this :
And, indeed, rewriting your code to this :
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
anddisplay_errors
options in your php.ini file.Or, if you don't have access to that one, you can use
error_reporting
andini_set
-- something like this at the beginning of your script might do :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.我不知道你是否可以做这样的事情。
首先执行包含,然后执行包含文件中任何给定变量的回显。
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.
我认为你需要做这样的事情:
“include”指令不会返回任何内容,因此尝试将其附加到字符串中是行不通的。
I think you would need to do something like this:
the "include" directive doesn't return anything, so trying to append it to a string is not going to work.
include
不是一个函数,而是一种特殊的语言构造。这也是为什么您不需要在“参数”两边加上括号的原因,因为include
只有一个“参数”。将其括在括号中就像将任何其他值括在括号中一样:相反,您需要将整个构造括在括号中:
但是由于
include
不会返回所包含脚本文件的输出,而是直接打印它,因此您可以需要缓冲输出并将其返回到包含该文件的脚本:这可以工作。但我认为你不想这样做。所以这可能更容易:
顺便说一句:
echo
、print
、exit
和die
是特殊的语言结构也。另请参阅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” asinclude
only has one “parameter”. Wrapping that in parenthesis is like wrapping any other value in parenthesis:Instead you need to wrap the whole construct in parenthesis:
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:That would work. But I don’t think that you want to do that. So this is probably quite easier:
By the way:
echo
,print
,exit
anddie
are special language constructs too. See also require_once () or die() not working要输出 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
file.php
中有什么内容吗?它可能是空的,或者其中的某些内容可能导致错误。Is there anything in
file.php
? It may be empty, or something in it may be causing an error.在浏览器中查看源代码,而不仅仅是可见部分。通常您可以通过按 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.我从未想过将 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.
file.php 末尾是否有多余的换行符?
Is there an extra line break at the end of file.php?