采访问题:我们可以在标题之前有回声吗? [英] Interview Question: Can we have an echo before header?

查看:71
本文介绍了采访问题:我们可以在标题之前有回声吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我出现了php测试,他们被问了一个问题,我找不到答案。



问题是这样的。

  echoMESSI受伤!!; 
header(Location:somepage.php);

访访者需要标头回音写在同一页上。



我不知道它有什么可能。它应该给出一些错误,如

< (输出开始于.....



是否真的有可能编写echo和header到相同的页面!!!

解决方案

您可以使用输出缓冲区

  ob_start(); 
echoMESSI is injured !!;
header(Location:somepage.php);
ob_end_flush();

问题是我们在开始发送输出后无法发送头文件,为了解决这个问题,我们缓冲了输出,函数 ob_start 打开输出缓冲,当输出缓冲处于活动状态时,脚本(标题除外)不会输出任何输出,而是e输出存储在内部缓冲区中。因此, echo 输出将被缓存。接下来我们发送头没有任何问题,因为我们还没有吐出任何输出。最后,我们调用 ob_end_flush 来清空内部缓冲区内容并停止输出缓冲。


I appeared for php test, their I was asked one question for which I could not find the answer.

The question is like this.

echo "MESSI is injured!!";
header("Location:somepage.php");

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

headers already sent by (output started at .....

Is it really possible to write echo and header onto same page !!!

解决方案

You can use Output Buffering as

ob_start();
echo "MESSI is injured!!";
header("Location:somepage.php");
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.

这篇关于采访问题:我们可以在标题之前有回声吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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