这是什么?
这是关于您在编写 PHP 程序时可能遇到但不知道如何修复它们的警告、错误和通知的大量答案。这也是一个社区 Wiki,因此邀请所有人参与添加和维护此列表。
这是为什么?
类似 "Headers already sent" 的问题或 "Calling a member of a non-object"经常在 Stack Overflow 上弹出。这些问题的根本原因总是相同的。因此,这些问题的答案通常会重复它们,然后向 OP 显示在他们的特定情况下要更改哪一行。这些答案不会为网站增加任何值(value),因为它们仅适用于 OP 的特定代码。具有相同错误的其他用户无法轻易地从中读取解决方案,因为他们太本地化了。这很可悲,因为一旦您了解了根本原因,修复错误就变得微不足道了。因此,此列表试图以通用的应用方式解释解决方案。
我应该在这里做什么?
如果您的问题已被标记为与该问题重复,请在下方找到您的错误消息并将修复应用到您的代码。答案通常包含进一步的调查链接,以防仅从一般答案中看不清楚。
如果你想贡献,请添加你“最喜欢的”错误消息、警告或通知,每个答案一个,简短描述它的含义(即使它只是在他们的手册页上突出显示术语),一个可能的解决方案或调试方法和现有的有值(value)的问答列表。另外,请随时改进任何现有答案。
列表
- Nothing is seen. The page is empty and white. (也称为 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又名
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: Maximum execution time of XX seconds exceeded
- 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() '
- Return type of AAA::BBB() should either be compatible with CCC::BBB(), or the #[\ReturnTypeWillChange] attribute should be used
- 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 (< PHP 8.0) 或 Warning: Array to string conversion (>= PHP 8.0)
- Notice: Trying to get property of non-object error
- Notice: Undefined variable or property
- "Notice: Undefined Index", or "Warning: Undefined array key"
- Notice: Undefined offset XXX [引用]
- Notice: Uninitialized string offset: XXX
- Notice: Use of undefined constant XXX - assumed 'XXX' / Error: Undefined constant 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
另见:
最佳答案
警告:无法修改 header 信息 - header 已发送
当您的脚本尝试向客户端发送 HTTP header 但之前已经有输出时发生,这导致 header 已经发送到客户端。
这是一个 E_WARNING
它不会停止脚本。
一个典型的例子是这样的模板文件:
<html>
<?php session_start(); ?>
<head><title>My Page</title>
</html>
...
session_start()
函数将尝试将带有 session cookie 的 header 发送到客户端。但是 PHP 在编写 <html>
时已经发送了 header 。元素到输出流。你必须移动 session_start()
到顶部。
您可以通过检查触发警告的代码之前 行并检查它的输出位置来解决此问题。将任何 header 发送代码移到该代码之前。
一个经常被忽视的输出是 PHP 关闭后的新行 ?>
.省略 ?>
被认为是标准做法当它是文件中的最后一件事时。同样,此警告的另一个常见原因是打开 <?php
时。前面有空格、行或不可见字符,导致 Web 服务器发送 header 和空格/换行符,因此当 PHP 开始解析时将无法提交任何 header 。
如果你的文件有多个<?php ... ?>
其中的代码块,它们之间不应有任何空格。 (注意:如果您有自动构造的代码,您可能有多个 block )
还要确保您的代码中没有任何字节顺序标记,例如,当脚本的编码为带 BOM 的 UTF-8 时。
相关问题:
关于php - 引用 - 这个错误在 PHP 中意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13803261/