开发者

Debugging PHP Output

I have a php website that on certai开发者_StackOverflow中文版n 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


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


Maybe it is a BOM character?


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜