php - 引用 - 这个错误在 PHP 中是什么意思?

标签 php arrays debugging error-handling warnings

这是什么?
这是一些关于您在编写 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. (也称为白页/死亡屏幕)
  • 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: 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
  • 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

  • 另见:
  • Reference - What does this symbol mean in PHP?
  • 最佳答案

    警告:无法修改 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 时。

    相关问题:
  • Headers already sent by PHP
  • All PHP "Headers already sent" Questions on Stackoverflow
  • Byte Order Mark
  • What PHP Functions Create Output?
  • 关于php - 引用 - 这个错误在 PHP 中是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39428087/

    相关文章:

    c++ - 将 int 的单个数字存储在数组中

    debugging - IntelliJ : Is it possible to start debugger with breakpoints muted?

    asp.net-mvc - 调试流畅的验证规则

    php - 如何在 Prestashop 中为 CMS 页面添加特色图片

    javascript - ReactJs:从 map 函数渲染的 li 到达方法后,target.key 未定义

    c - 将结构体 typedef 为数组意味着什么?

    c++ - 在调试器内的 OS X 上启动 C++ 命令行工具的简单方法

    php - CakePHP 向表中添加列

    php - 检查 PDO 准备()、执行()是否至少返回一行

    php - 为每个系列创建一个下拉菜单