参考 - 这个错误在 PHP 中意味着什么? [英] Reference - What does this error mean in PHP?

查看:28
本文介绍了参考 - 这个错误在 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.

  • 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆