Interview Question: Can we have an echo before header?(面试问题:我们可以在标题之前有一个回声吗?)
问题描述
我参加了 php 测试,他们问了一个我找不到答案的问题.
I appeared for php test, their I was asked one question for which I could not find the answer.
问题是这样的.
echo "MESSI is injured!!";
header("Location:somepage.php");
面试官希望 header 和 echo 写在同一页面上.
Interviewer want both header and echo to be written on the same page.
我想知道这怎么可能.它应该给出一些错误,例如
I wonder how's it possible.It should give some error like
已发送的标头(输出开始于.....
真的可以将echo和header写到同一个页面吗!!!
Is it really possible to write echo and header onto same page !!!
推荐答案
你可以使用 输出缓冲 as
ob_start();
echo "MESSI is injured!!";
header("Location:somepage.php");
ob_end_flush();
问题是我们开始发送输出后无法发送标头.为了解决这个问题,我们缓冲输出.ob_start
函数打开输出缓冲.当输出缓冲处于活动状态时,不会从脚本发送输出(除了标题),而是将输出存储在内部缓冲区中.所以 echo
输出将被缓冲.接下来我们发送标题没有任何问题,因为我们还没有吐出任何输出.最后我们调用 ob_end_flush
来刷新内部缓冲区内容并停止输出缓冲.
The problem is that we cannot send the header after we start sending the output. To solve this we buffer the output. The function ob_start
turns output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer. So the echo
output will be buffered. Next we send the header without any problem as we've not yet spit out any output. Finally we call ob_end_flush
to flush the internal buffer contents and to stop output buffering.
这篇关于面试问题:我们可以在标题之前有一个回声吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:面试问题:我们可以在标题之前有一个回声吗?


- 从 PHP 中的输入表单获取日期 2022-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- Laravel 仓库 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01