参考 - 这个错误在 PHP 中意味着什么? [英] Reference - What does this error mean in 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.
- 什么也看不见.页面是空白的,是白色的.(也称为白页/死机屏幕)
- 代码无法运行/我的 PHP 代码部分看起来像是输出了
- 警告:无法修改标头信息 - 标头已发送
- 警告:mysql_fetch_array() 期望参数 1 是资源,给定的布尔值 又名
警告:mysql_fetch_array():提供的参数不是有效的 MySQL 结果资源 - 警告:[function] 期望参数 1 是资源,给定布尔值
- 警告:[function]:无法打开流:[reason]
- 警告: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 的对象转换为字符串
- 致命错误:未定义的类常量
- 致命错误:未捕获的类型错误:参数 #n 必须是 x、y 类型
- 注意:数组到字符串的转换
- 注意:尝试获取非对象属性错误
- 注意:未定义的变量或属性
- 注意:未定义索引
- 注意:未定义偏移量XXX [参考]
- 注意:未初始化的字符串偏移量:XXX
- 注意:使用未定义的常量 XXX - 假定为XXX"
- MySQL:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解要在附近使用的正确语法...在行...
- 严格的标准:非静态方法[<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
另见:
推荐答案
警告:无法修改标头信息 - 标头已发送
当您的脚本尝试向客户端发送 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 在将 元素写入输出流时已经发送了标头.您必须将
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 开始解析时将无法提交任何标头.
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 中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!