Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Build a Basic PHP Website (2018) Adding a Basic Form Redirecting After a Form Submission

Zijun Liao
Zijun Liao
13,409 Points

Send string to browser before redirection

In this video, Alena mentioned that we should not send any string to the browser before the redirection, otherwise it causes an error.

I just wondering what does sending a string to browser means, like echo anything? I tried echo "XXX", but it still works as usual.

Cliff Jackson
Cliff Jackson
2,886 Points

I am also on this part at the moment and it's a little confusing as a lot of how she explains things is very vague. One of the questions in the section after had an echo statement before the redirect which the answer was an error would be shown although the echo statement would still show.

1 Answer

Ricardo Vermaat
Ricardo Vermaat
150 Points

When you redirect using a header() function you cannot already have headers sent to the browser prior to this function.

For example:

echo 'Hello World!';
header('Location: teamtreehouse.com');

Will result in a warning:

PHP Warning: Cannot modify header information - headers already sent by (output started at / home/treehouse/workspace/index.php:2) in /home/treehouse/workspace/index.php on line 3

Here you can read more about sending headers

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.