Alex Rivera | Logout

Interview Question: Can we have an echo before header?

Asked 2010-09-22T04:36:52.873
14

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

Edit
Report

1 Answer

2

You can do it as long as all the header calls come before any non-header output is sent (this includes pesky things like newlines/whitespace). So

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

should do the trick

answered 2010-09-22T04:48:43.473

Your Answer