本文翻译自:Reference - What does this error mean in PHP?
What is this? 这是什么?
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. 这是有关警告,错误和注意事项的许多答案,这些警告,错误和注意事项在您对PHP进行编程时可能会遇到,并且不知道如何解决它们。 This is also a Community Wiki, so everyone is invited to participate adding to and maintaining this list. 这也是一个社区Wiki,因此邀请所有人参与添加并维护此列表。
Why is this? 为什么是这样?
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. 因此,这些问题的答案通常会重复它们,然后向OP显示在特定情况下应更改的行。 These answers do not add any value to the site because they only apply to the OP's particular code. 这些答案不会为网站增加任何价值,因为它们仅适用于OP的特定代码。 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. 因此,此列表试图以一种通用的方式解释该解决方案。
What should I do here? 我该怎么办?
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. 此外,请随时改善任何现有答案。
The List 列表
- 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 代码未运行/输出了我的PHP代码的一部分
- Warning: Cannot modify header information - headers already sent 警告:无法修改标头信息-标头已发送
- Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given aka 警告:mysql_fetch_array()期望参数1为资源,给定布尔值
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource aka 警告:mysql_fetch_array():提供的参数不是有效的MySQL结果资源
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given (or similar variations) 警告:mysqli_num_rows()期望参数1为mysqli_result,给定布尔值 (或类似的变体) - Warning: [function] expects parameter 1 to be resource, boolean given 警告: [函数]期望参数1为资源,给定布尔值
- Warning: [function] : failed to open stream: [reason] 警告: [功能] :无法打开流: [原因]
- Warning: open_basedir restriction in effect 警告:open_basedir限制生效
- Warning: Division by zero 警告:除以零
- Warning: Illegal string offset 'XXX' 警告:字符串偏移量“ XXX”非法
- Warning: count(): Parameter must be an array or an object that implements Countable 警告:count():参数必须是实现Countable的数组或对象
- Parse error: syntax error, unexpected '[' 解析错误:语法错误,意外的'['
- Parse error: syntax error, unexpected T_XXX 解析错误:语法错误,意外的T_XXX
- Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE 解析错误:语法错误,意外的T_ENCAPSED_AND_WHITESPACE
- Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM 解析错误:语法错误,意外T_PAAMAYIM_NEKUDOTAYIM
- Parse error: syntax error, unexpected 'require_once' (T_REQUIRE_ONCE), expecting function (T_FUNCTION) 解析错误:语法错误,意外的'require_once'(T_REQUIRE_ONCE),预期函数(T_FUNCTION)
- Parse error: syntax error, unexpected T_VARIABLE 解析错误:语法错误,意外的T_VARIABLE
- Fatal error: Allowed memory size of XXX bytes exhausted (tried to allocate XXX bytes) 致命错误:耗尽了XXX个字节的允许的内存大小(尝试分配XXX个字节)
- Fatal error: Call to a member function ... on a non-object or null 致命错误:在非对象上调用成员函数...或为null
- Fatal Error: Call to Undefined function XXX 严重错误:调用未定义函数XXX
- Fatal Error: Cannot redeclare XXX 严重错误:无法重新声明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() ' 致命错误:AAA :: BBB()的声明必须与CCC :: BBB()的声明兼容
- Fatal error: Using $this when not in object context 致命错误:不在对象上下文中时使用$ this
- Fatal error: Object of class Closure could not be converted to string 致命错误:类Closure的对象无法转换为字符串
- 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] 注意:未定义的偏移XXX [参考]
- Notice: Uninitialized string offset: XXX 注意:未初始化的字符串偏移量:XXX
- Notice: Use of undefined constant XXX - assumed 'XXX' 注意:使用未定义常数XXX-假定为“ XXX”
- MySQL: You have an error in your SQL syntax; MySQL:SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near ... at line ... 检查与您的MySQL服务器版本相对应的手册,以在第...行附近使用正确的语法。
- Strict Standards: Non-static method [<class>::<method>] should not be called statically 严格标准:非静态方法[<class> :: <method>]不应被静态调用
- Warning: function expects parameter X to be boolean/string/integer 警告:函数期望参数X为布尔值/字符串/整数
- HTTP Error 500 - Internal server error HTTP错误500-内部服务器错误
Also, see: 另请参阅:
#1楼
参考:https://stackoom.com/question/ra3S/参考-此错误在PHP中意味着什么
#2楼
Fatal error: Call to a member function ... on a non-object 致命错误:在非对象上调用成员函数...
Happens with code similar to xyz->method()
where xyz
is not an object and therefore that method
can not be called. 发生类似于xyz->method()
代码,其中xyz
不是对象,因此无法调用该method
。
This is a fatal error which will stop the script (forward compatibility notice: It will become a catchable error starting with PHP 7). 这是一个致命错误,它将停止脚本(向前兼容性通知:从PHP 7开始,它将成为可捕获的错误)。
Most often this is a sign that the code has missing checks for error conditions. 大多数情况下,这表明代码缺少对错误条件的检查。 Validate that an object is actually an object before calling its methods. 在调用对象的方法之前,先验证该对象实际上是对象。
A typical example would be 一个典型的例子是
// ... some code using PDO
$statement = $pdo->prepare('invalid query', ...);
$statement->execute(...);
In the example above, the query cannot be prepared and prepare()
will assign false
to $statement
. 在上面的示例中,查询无法准备, prepare()
将false
分配给$statement
。 Trying to call the execute()
method will then result in the Fatal Error because false
is a "non-object" because the value is a boolean. 然后尝试调用execute()
方法将导致致命错误,因为false
是“非对象”,因为值是布尔值。
Figure out why your function returned a boolean instead of an object. 弄清楚函数为什么返回布尔值而不是对象。 For example, check the $pdo
object for the last error that occurred. 例如,检查$pdo
对象以查找最后发生的错误。 Details on how to debug this will depend on how errors are handled for the particular function/object/class in question. 有关如何进行调试的详细信息将取决于所处理的特定功能/对象/类的错误处理方式。
If even the ->prepare
is failing then your $pdo
database handle object didn't get passed into the current scope . 如果->prepare
失败,那么您的$pdo
数据库句柄对象也不会传递到当前范围内 。 Find where it got defined. 查找它的定义位置。 Then pass it as a parameter, store it as property, or share it via the global scope. 然后将其作为参数传递,将其存储为属性或通过全局范围共享。
Another problem may be conditionally creating an object and then trying to call a method outside that conditional block. 另一个问题可能是有条件地创建对象,然后尝试在该条件块之外调用方法。 For example 例如
if ($someCondition) {
$myObj = new MyObj();
}
// ...
$myObj->someMethod();
By attempting to execute the method outside the conditional block, your object may not be defined. 通过尝试在条件块之外执行该方法,可能未定义您的对象。
Related Questions: 相关问题:
- Call to a member function on a non-object 调用非对象上的成员函数
- List all PHP "Fatal error: Call to a member function ... on a non-object" Questions on Stackoverflow 列出所有PHP“致命错误:在非对象上调用成员函数...” Stackoverflow上的问题
#3楼
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given 警告:mysql_fetch_array()期望参数1为资源,给定布尔值
First and foremost: 首先,最重要的是:
Please, don't use
mysql_*
functions in new code . 请不要在新代码中使用mysql_*
函数 。 They are no longer maintained and are officially deprecated . 它们不再维护,已正式弃用 。 See the red box ? 看到红色框了吗? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. 相反,要了解准备好的语句 ,并使用PDO或MySQLi- 本文将帮助您确定哪一个。 If you choose PDO, here is a good tutorial . 如果您选择PDO, 这是一个很好的教程 。
This happens when you try to fetch data from the result of mysql_query
but the query failed. 当您尝试从mysql_query
的结果中获取数据但查询失败时,会发生这种情况。
This is a warning and won't stop the script, but will make your program wrong. 这是一个警告,不会停止脚本,但是会使您的程序出错。
You need to check the result returned by mysql_query
by 您需要通过以下方式检查mysql_query
返回的结果
$res = mysql_query($sql);
if (!$res) {
die(mysql_error());
}
// after checking, do the fetch
Related Questions: 相关问题:
- mysql_fetch_array() expects parameter 1 to be resource, boolean given in select mysql_fetch_array()期望参数1为资源,在选择中给定布尔值
- All "mysql_fetch_array() expects parameter 1 to be resource, boolean given" Questions on Stackoverflow 所有“ mysql_fetch_array()期望参数1是资源,给定布尔值”
Related Errors: 相关错误:
Other mysql*
functions that also expect a MySQL result resource as a parameter will produce the same error for the same reason. 出于同样的原因,也希望将MySQL结果资源用作参数的其他mysql*
函数将产生相同的错误。
#4楼
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. 当脚本尝试将HTTP标头发送到客户端但之前已经有输出时,会发生此情况,这导致标头已经发送到客户端。
This is an E_WARNING
and it will not stop the script. 这是一个E_WARNING
,它不会停止脚本。
A typical example would be a template file like this: 一个典型的示例是这样的模板文件:
<html>
<?php session_start(); ?>
<head><title>My Page</title>
</html>
...
The session_start()
function will try to send headers with the session cookie to the client. session_start()
函数将尝试将带有会话cookie的标头发送到客户端。 But PHP already sent headers when it wrote the <html>
element to the output stream. 但是PHP在将<html>
元素写入输出流时已经发送了标头。 You'd have to move the session_start()
to the top. 您必须将session_start()
移到顶部。
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. 将任何标头发送代码移到该代码之前。
An often overlooked output is new lines after PHP's closing ?>
. 一个经常被忽略的输出是PHP关闭?>
之后的换行符。 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
有空格,行或不可见字符,导致Web服务器发送标头和空白/换行符,因此当PHP开始解析时将不会能够提交任何标题。
If your file has more than one <?php ... ?>
code block in it, you should not have any spaces in between them. 如果文件中包含多个<?php ... ?>
代码块,则它们之间不应有任何空格。 (Note: You might have multiple blocks if you had code that was automatically constructed) (注意:如果您具有自动构造的代码,则可能有多个块)
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. 还要确保您的代码中没有任何字节顺序标记,例如,当脚本的编码为带有BOM的UTF-8时。
Related Questions: 相关问题:
- Headers already sent by PHP 标题已由PHP发送
- All PHP "Headers already sent" Questions on Stackoverflow 关于Stackoverflow的所有PHP“标题已发送”问题
- Byte Order Mark 字节顺序标记
- What PHP Functions Create Output? 哪些PHP函数创建输出?
#5楼
Parse error: syntax error, unexpected T_XXX 解析错误:语法错误,意外的T_XXX
Happens when you have T_XXX
token in unexpected place, unbalanced (superfluous) parentheses, use of short tag without activating it in php.ini, and many more. 当您将T_XXX
令牌放置在意外的地方,不平衡的(多余的)括号,在php.ini中未使用短标签而不激活它的情况下,就会发生这种情况。
Related Questions: 相关问题:
- Reference: PHP Parse/Syntax Errors; 参考: PHP解析/语法错误; and How to solve them? 以及如何解决它们?
- Parse Error: syntax error: unexpected '{' 解析错误:语法错误:意外的“ {”
- Parse error: Syntax error, unexpected end of file in my PHP code 解析错误:语法错误,我的PHP代码中文件意外结束
- Parse error: syntax error, unexpected '<' in - Fix? 解析错误:语法错误,输入中出现意外的'<'-已修复?
- Parse error: syntax error, unexpected '?' 解析错误:语法错误,意外的“?”
For further help see: 有关更多帮助,请参见:
- http://phpcodechecker.com/ - Which does provide some more helpful explanations on your syntax woes. http://phpcodechecker.com/-确实为您的语法问题提供了更多有用的解释。
#6楼
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM 解析错误:语法错误,意外T_PAAMAYIM_NEKUDOTAYIM
The scope resolution operator is also called "Paamayim Nekudotayim" from the Hebrew פעמיים נקודתיים. 示波器解析算子也被称为“ Paamayim Nekudotayim”,来自希伯来语פעמייםנקודתיים。 which means "double colon". 意思是“双冒号”。
This error typically happens if you inadvertently put ::
in your code. 如果您无意中将::
放在代码中,通常会发生此错误。
Related Questions: 相关问题:
- Reference: PHP Parse/Syntax Errors; 参考: PHP解析/语法错误; and How to solve them? 以及如何解决它们?
- What do two colons mean in PHP? PHP中的两个冒号是什么意思?
- What's the difference between :: (double colon) and -> (arrow) in PHP? PHP中的::(双冒号)和->(箭头)有什么区别?
- Unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting T_NS_Separator 意外的T_PAAMAYIM_NEKUDOTAYIM,预期为T_NS_Separator
Documentation: 说明文件: