Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

include() and blank lines before header()?

In the PHP Manual page for header():

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. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Well, in the code block from this video there are both include() and blank lines before the header() function:

<?php 
include("inc/data.php");
include("inc/functions.php");

if (isset($_GET["id"])) { 
    $id = $_GET["id"];
    if (isset($catalog[$id])) {
        $item = $catalog[$id];
    }
}

if (!isset($item)) {
    header("location:catalog.php");
    exit;
}

Is this lesson an example of bad code or am I misunderstanding something? Thanks for any insight.

2 Answers

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

before any actual output is sent - if you look in the code you have shared you can see that there is not being outputted any data to the screen (i.e. echo something).

Thanks.

Thanks @Henrik Christensen

This is the sentence that confused me most so let me check if I understood it now:

It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called.

The mention of the include and require functions is not because of those functions per se. Its because the external files might contain output in one of those forms.

Empty lines and spaces between PHP statements have no meaning, so they're not the problem. The problem, for instance, would be an empty space left after a PHP closing tag, that would be html output.

This is right?