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
-
5Could you provide some code of a problematic page?Michael Irigoyen– Michael Irigoyen2011-01-25 15:42:24 +00:00Commented Jan 25, 2011 at 15:42
-
1Or could you provide a link to a URLrobjmills– robjmills2011-01-25 15:47:41 +00:00Commented Jan 25, 2011 at 15:47
-
Firebug web developer extension might helpPhill Pafford– Phill Pafford2011-01-25 15:50:54 +00:00Commented Jan 25, 2011 at 15:50
5 Answers
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
11 Comments
?>. EVER.?> in files that contain only PHP code, according to PHP's own developers. CodeIgniter and Drupal agree, and so do many other developers.?> 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/…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
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.