Apache or PHP generating a line feed character

I am trying to create an XML file in a PHP web application:

<?php ... header('Content-Type: application/xml'); header('Content-Disposition: attachment; filename=test.xml'); echo "<?xml version=\"1.0\"?>\r\n" . ... 

When using my servers (PHP version 5.3.8 / Apache 2.2.17 and PHP version 5.3.10-1 / Apache 2.2.22 respectively), a line character (hex 0a ) is entered at the beginning of the output , which leads to invalid XML, which cannot to be used. There, another online question about this is unresolved.

So, if I try echo "bug"; I get 4 bytes, not 3: 0a 62 75 67

However, when using WAMP server locally (PHP 5.4.3 / Apache 2.4.2), I get 3 bytes: 62 75 67 .

  • Is this a known bug / feature?
  • Is this a configuration problem?
  • What is to blame, Apache or PHP?
  • Do I need to update my servers? I would rather not.
+4
source share
4 answers

It seems that the problem with 0a was caused by the alternation of the Enter character in the PHP file included in my main PHP file. When I deleted it from the included file, the 0a character in my output disappeared.

What I find special is the different handling of spaces between the versions of PHP that I experienced, and the fact that I still got 0a when testing community suggestions.

I don’t have more time to investigate this, but my advice to people experiencing similar problems is to check if spaces in include files can play the equation. Also, avoid ending the <?php tag as suggested by Dan below.

+3
source

I just ran into this. Took me and the team an hour to track down. This is amazing. I love PHP so much.

This is what caused it for us:

 <php $code = "blah"; ?php> <EOF> 

NEWLINE NOTE AT THE END OF A FILE

PHP, in all its transcendental beauty, will interpret this new line as something that you are actually trying to send to someone by wire. After all, this is HTML. As a result, the maximum of your newline character will be added to your answer, even if several PHP files in your include path end with a new line.

Pay special attention to the fact that any file that you include or require, if it ends on a new line, everything that you echo or print from your current file will be added to a new line.

Just. ASTONISHING.

+3
source

Perhaps this is an encoding problem. If you use UTF8 with a specification , there is an extra character at the beginning of the files. Check the encoding of your files and convert it to UTF8 without specification to avoid this extra character.

+2
source

In my case, it was a line feed at the beginning

enter image description here

0
source

All Articles