1

I have a php website that on certain pages is adding a dot or space before the first html tag. I can't figure out where it is coming from - is there a way to debug the code so i can see where it is coming from? Thanks, Josh

3
  • 5
    Could you provide some code of a problematic page? Commented Jan 25, 2011 at 15:42
  • 1
    Or could you provide a link to a URL Commented Jan 25, 2011 at 15:47
  • Firebug web developer extension might help Commented Jan 25, 2011 at 15:50

5 Answers 5

2

To help prevents this happening it is considered a good practice to don't end your PHP file with a ?>.

You possibly have some file that are this way (notice the extra space after the ?>):

<?php
    // Some code //
?>   

If you would remove the ?> at the end, the extra space at the end of the file won't be interpreted as something to output.

For files that contain only PHP code, the closing tag ("?>") is never permitted. It is not required by PHP, and omitting it´ prevents the accidental injection of trailing white space into the response.

Source: http://framework.zend.com/manual/en/coding-standard.php-file-formatting.html

Sign up to request clarification or add additional context in comments.

11 Comments

-1 Awful, horrible advise. Under no circumstance should you ever not include the ending ?>. EVER.
@Michael - Why? This doesn't really qualify as an explanation to justify your downvote!
@Michael Why do you say this? I don't know if I necessarily agree with that. In fact I think the Zend coding standard says to leave it out on files that only contain php code.
@Michael Actually, you're completely wrong. You shouldn't include the closing ?> in files that contain only PHP code, according to PHP's own developers. CodeIgniter and Drupal agree, and so do many other developers.
@Michael Irigoyen: Omitting the closing ?> is generally considered good practice, and it encouraged by a lot of coding standards. Including it makes you more prone to error. Omitting it is not being lazy, it's being smart. See also php.net/manual/en/…
|
1

Maybe it is a BOM character?

Comments

1

Maybe you should check your templates if you are using them... the problem could be there and not in your main code.

and yes is a GOOD PRACTICE in PHP not to close the ending tag.

1 Comment

Hey dont believe me.... framework.zend.com/manual/en/…
0

There really is no good way to go about debugging this. You need to go through every file the page is hitting and figure out where the output is coming from. If you really wanted to be lazy about it you could do some output buffering, but this isn't the right way to do things.

Comments

0

Problems like this can be difficult to track down. If you're in some kind of framework or system that includes a lot of files, you might try a var_dump(get_included_files()) on the line before your error occurs, and that will give you a place to start. If that isn't sufficient, xdebug might get you further. Things to look out for are space before and after the PHP tags, and functions that might send output.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.