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

查看:288
本文介绍了参考 - 这个错误在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. This is also a Community Wiki, so everyone is invited to participate in 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 his/her 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 can not 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, 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.

如果您要贡献,请添加您的最爱错误消息,警告或通知,每个答案一个简短的描述,这意味着什么(即使它只是强调他们的手册页面的条款),一个可能的解决方案或调试方法和现有的Q& A的列表是有价值的。另外,请随时改进任何现有的答案。

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 a.k.a.
    Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given (or similar variations)
  • 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'
  • 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 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
  • 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
  • Notice: Array to string conversion
  • Notice: Trying to get property of non-object error
  • Notice: Undefined variable
  • 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

另见

  • Reference - What does this symbol mean in PHP?

推荐答案

警告:无法修改标题信息 - 已发送的标题



您的脚本尝试向客户端发送一个HTTP头,但是之前已经输出了,这导致头文件已经发送给客户端。

Warning: Cannot modify header information - headers already sent

Happens when your script tries to send a 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在向输出流写入< html> 元素时已经发送了标题。您必须将 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 之前有一个空的空格,行或不可见的字符,导致网络服务器发送头文件因此,当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 webserver 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.

相关问题:

  • Headers already sent by PHP
  • All PHP "Headers already sent" Questions on Stackoverflow
  • Byte Order Mark
  • What PHP Functions Create Output?

这篇关于参考 - 这个错误在PHP中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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