参考 - 这个错误在 PHP 中是什么意思?
这是一些关于您在编写 PHP 时可能遇到的警告、错误和通知的答案,但不知道如何修复它们.这也是一个社区 Wiki,因此邀请所有人参与添加和维护此列表.
This is a number of answers about warnings, errors, and notices you might encounter while programming PHP and have no clue how to fix them. This is also a Community Wiki, so everyone is invited to participate adding to and maintaining this list.
标头已发送" 或 调用非对象的成员" 经常在 Stack Overflow 上弹出.这些问题的根本原因总是相同的.因此,这些问题的答案通常会重复它们,然后向 OP 显示在其特定情况下要更改哪一行.这些答案不会为站点增加任何价值,因为它们仅适用于 OP 的特定代码.其他具有相同错误的用户无法轻松地从中读取解决方案,因为他们过于本地化.这很可悲,因为一旦您了解了根本原因,修复错误就变得微不足道了.因此,此列表试图以通用的方式解释解决方案.
Questions like "Headers already sent" or "Calling a member of a non-object" pop up frequently on Stack Overflow. The root cause of those questions is always the same. So the answers to those questions typically repeat them and then show the OP which line to change in their particular case. These answers do not add any value to the site because they only apply to the OP's particular code. Other users having the same error cannot easily read the solution out of it because they are too localized. That is sad because once you understood the root cause, fixing the error is trivial. Hence, this list tries to explain the solution in a general way to apply.
如果您的问题已被标记为与此问题重复,请在下方找到您的错误消息并将修复应用于您的代码.答案通常包含进一步调查的链接,以防仅从一般答案中不清楚.
If your question has been marked as a duplicate of this one, please find your error message below and apply the fix to your code. The answers usually contain further links to investigate in case it shouldn't be clear from the general answer alone.
如果您想投稿,请添加您的收藏"错误消息、警告或通知、每个答案一个、简短说明其含义(即使只是在其手册页中突出显示术语)、可能的解决方案或调试方法以及有价值的现有问答列表.此外,请随时改进任何现有答案.
If you want to contribute, please add your "favorite" error message, warning or notice, one per answer, a short description what it means (even if it is only highlighting terms to their manual page), a possible solution or debugging approach and a listing of existing Q&A that are of value. Also, feel free to improve any existing answers.
- 什么也没看到.页面是空白的,是白色的.(也称为White Page/Screen Of Death)
- 代码无法运行/输出了我的 PHP 代码的部分内容
- 警告:无法修改标头信息 - 标头已发送
- 警告:mysql_fetch_array() 期望参数 1 是资源,给定的布尔值 a.k.a.
警告:mysql_fetch_array():提供的参数不是有效的 MySQL 结果资源 - 警告:[function] 期望参数 1 是资源,给定布尔值
- 警告:[功能]:无法打开流:[原因]
- 警告:open_basedir 限制生效
- 警告:除以零
- 警告:非法字符串偏移 'XXX'
- 警告:count():参数必须是数组或实现了Countable的对象
- 解析错误:语法错误,意外'['
- 解析错误:语法错误,意外的 T_XXX
- 解析错误:语法错误,意外的 T_ENCAPSED_AND_WHITESPACE
- 解析错误:语法错误,意外的 T_PAAMAYIM_NEKUDOTAYIM
- 解析错误:语法错误,意外的'require_once'(T_REQUIRE_ONCE),期望函数(T_FUNCTION)
- 解析错误:语法错误,意外的 T_VARIABLE
- 致命错误:允许的 XXX 字节内存大小已用尽(尝试分配 XXX 字节)
- 致命错误:在非对象或 null 上调用成员函数...
- 致命错误:调用未定义函数 XXX
- 致命错误:无法重新声明 XXX
- 致命错误:无法在写入上下文中使用函数返回值
- 致命错误:AAA::BBB() 的声明必须与 CCC::BBB() 的声明兼容'
- 致命错误:不在对象上下文中使用 $this
- 致命错误:Closure 类的对象无法转换为字符串
- 致命错误:未定义的类常量
- 致命错误:未捕获的TypeError:参数#n必须是x类型,y给定
- 注意:数组到字符串的转换
- 注意:试图获取非对象错误的属性
- 注意:未定义的变量或属性
- 注意:未定义索引
- 注意:未定义的偏移量 XXX [参考]
- 注意:未初始化的字符串偏移量:XXX
- 注意:使用未定义的常量 XXX - 假定为XXX"
- MySQL:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法以使用 near ... at line ...
- 严格标准:非静态方法[<class>::<method>]静态调用
- 警告:函数要求参数 X 为布尔值/字符串/整数
- HTTP 错误 500 - 内部服务器错误
- 已弃用:带花括号的数组和字符串偏移访问语法已弃用
- Nothing is seen. The page is empty and white. (also known as White Page/Screen Of Death)
- Code doesn't run/what looks like parts of my PHP code are output
- Warning: Cannot modify header information - headers already sent
- Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given a.k.a.
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource - Warning: [function] expects parameter 1 to be resource, boolean given
- Warning: [function]: failed to open stream: [reason]
- Warning: open_basedir restriction in effect
- Warning: Division by zero
- Warning: Illegal string offset 'XXX'
- Warning: count(): Parameter must be an array or an object that implements Countable
- Parse error: syntax error, unexpected '['
- Parse error: syntax error, unexpected T_XXX
- Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE
- Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM
- Parse error: syntax error, unexpected 'require_once' (T_REQUIRE_ONCE), expecting function (T_FUNCTION)
- Parse error: syntax error, unexpected T_VARIABLE
- Fatal error: Allowed memory size of XXX bytes exhausted (tried to allocate XXX bytes)
- Fatal error: Call to a member function ... on a non-object or null
- Fatal Error: Call to Undefined function XXX
- Fatal Error: Cannot redeclare XXX
- Fatal error: Can't use function return value in write context
- Fatal error: Declaration of AAA::BBB() must be compatible with that of CCC::BBB()'
- Fatal error: Using $this when not in object context
- Fatal error: Object of class Closure could not be converted to string
- Fatal error: Undefined class constant
- Fatal error: Uncaught TypeError: Argument #n must be of type x, y given
- Notice: Array to string conversion
- Notice: Trying to get property of non-object error
- Notice: Undefined variable or property
- Notice: Undefined Index
- Notice: Undefined offset XXX [Reference]
- Notice: Uninitialized string offset: XXX
- Notice: Use of undefined constant XXX - assumed 'XXX'
- MySQL: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ... at line ...
- Strict Standards: Non-static method [<class>::<method>] should not be called statically
- Warning: function expects parameter X to be boolean/string/integer
- HTTP Error 500 - Internal server error
- Deprecated: Arrays and strings offset access syntax with curly braces is deprecated
另见:
- 参考 - 这个符号在 PHP 中是什么意思?
推荐答案
警告:无法修改标头信息 - 标头已发送
当您的脚本尝试向客户端发送 HTTP 标头但之前已经有输出时会发生这种情况,这导致标头已经发送到客户端.
Warning: Cannot modify header information - headers already sent
Happens when your script tries to send an HTTP header to the client but there already was output before, which resulted in headers to be already sent to the client.
这是一个 E_WARNING
和它不会停止脚本.
This is an E_WARNING
and it will not stop the script.
一个典型的例子是这样的模板文件:
A typical example would be a template file like this:
<html>
<?php session_start(); ?>
<head><title>My Page</title>
</html>
...
session_start()
函数将尝试将带有会话 cookie 的标头发送到客户端.但是 PHP 在将 <html>
元素写入输出流时已经发送了标头.您必须将 session_start()
移到顶部.
The session_start()
function will try to send headers with the session cookie to the client. But PHP already sent headers when it wrote the <html>
element to the output stream. You'd have to move the session_start()
to the top.
您可以通过检查触发警告的代码之前行并检查其输出位置来解决此问题.将任何标头发送代码移到该代码之前.
You can solve this by going through the lines before the code triggering the Warning and check where it outputs. Move any header sending code before that code.
一个经常被忽视的输出是 PHP 结束 ?>
之后的新行.当 ?>
是文件中的最后一项时,省略它被认为是一种标准做法.同样,此警告的另一个常见原因是开头的 <?php
前面有空格、行或不可见字符,从而导致 Web 服务器发送标头和空格/换行符当 PHP 开始解析时将无法提交任何 header.
An often overlooked output is new lines after PHP's closing ?>
. It is considered a standard practice to omit ?>
when it is the last thing in the file. Likewise, another common cause for this warning is when the opening <?php
has an empty space, line, or invisible character before it, causing the web server to send the headers and the whitespace/newline thus when PHP starts parsing won't be able to submit any header.
如果您的文件中有多个 <?php ... ?>
代码块,则它们之间不应有任何空格.(注意:如果你有自动构建的代码,你可能有多个块)
If your file has more than one <?php ... ?>
code block in it, you should not have any spaces in between them. (Note: You might have multiple blocks if you had code that was automatically constructed)
还要确保您的代码中没有任何字节顺序标记,例如当脚本的编码是带有 BOM 的 UTF-8 时.
Also make sure you don't have any Byte Order Marks in your code, for example when the encoding of the script is UTF-8 with BOM.
相关问题:
- 已由 PHP 发送的标头
- 关于 Stackoverflow 的所有 PHP标头已发送"问题
- 字节顺序标记
- 什么 PHP 函数创建输出?
相关文章