PHP-“标头已发送"错误取决于输出长度?

2022-01-11 00:00:00 output header php

我有一个输出 mysql 单元数据的脚本.内容"单元格包含长度不同的文本输出.

I have a script which outputs mysql cell data. The "content" cell contains text output, which is of varied length.

当内容"单元格的内容很小(比如说,几行文本)时,一切正常.但是,当输出达到多个段落或更多段落时,我会收到标头已发送"错误.

When the contents of the "content" cell are small (lets say, a few lines of text), everything works fine. However, when the output reaches several paragraphs or more, I get the 'headers already sent' error.

它取决于输出长度吗?我在哪里可以阅读更多关于它的信息?我在 SO 上找到的答案没有提到这种输出长度依赖性.

Does it depend on the output length? Where can I read more about it? The answers I've found on SO mention nothing of such output length-dependency.

 44:   echo "
 45:       <p>".$article['content']."</p>
 46:   ";

如果内容"输出的大小很大,脚本会产生以下错误:

If the size of the 'content' output is large, the script produces the following error:

PHP 警告:无法修改标头信息 - 标头已发送通过(输出开始于/home/mantas/htdocs/asm/article.php:46)在/home/mantas/htdocs/asm/include/comments_class.php 第 56 行

PHP Warning: Cannot modify header information - headers already sent by (output started at /home/mantas/htdocs/asm/article.php:46) in /home/mantas/htdocs/asm/include/comments_class.php on line 56

推荐答案

如果你愿意,PHP 会缓冲输出.您可以使用 ob_start() 等以编程方式对此进行控制.但是,还有一个选项可以在 php.ini 中设置输出缓冲.

PHP will buffer output if you want it to. You can control this programmatically with ob_start(), etc. However, there is a further option to set output buffering on in php.ini.

设置 output_buffering=on 启用它,而设置 output_buffering=4096 将设置缓冲区大小限制.phpinfo() 应该告诉你这是否启用,以及缓冲区大小是多少.

Setting output_buffering=on enables it, while setting output_buffering=4096 will set a limit to the buffer size. phpinfo() should tell you if this is enabled, and what the buffer size is.

PHP 参考是这里

相关文章