4

I have spent the last several hours pulling my hair out trying to figure out the solution to this problem. I am sending an AJAX request which, up until some minor changes, worked perfectly, returning a lovely usable character to the Javascript. Now, however, a \r\n is being returned, and I have spent far too long tracking it down. My final method for finding where it was being included was literally echo-ing "OMG" in various places around my scripts until it showed up on Line 2 of the HTML instead of Line 1. Here is the offending script:

// Import Global Game Variables
include('../engine/engine_core_functions.php');

// Convert our gamestate(gameID)
//$curGamestate = getCurrentGamestate($gameID);

// Make sure it's a valid turn
if(isMyTurn()) {
    // Draw a card from the card drawing mechanism
    $cardValue = drawCard();
    $cardValue = str_replace("\r", 'R', $cardValue);
    echo $cardValue;
}
else echo 'Error 3';

The line skip occurs immediately after the include file at the top. Before the include, no line break, after the include, line break. So I go to the include file. Placing my

echo 'OMG!';

at the VERY END of the included file does NOT produce a line break. Which led me to believe that including a file may (why!?) generate a line break (it's 5 AM...). However, there are multiple included files at the top of the offending included file. None of them generate breaks. The entire "engine_core_functions.php" generates no line breaks at all.

However, a break shows up when it is included in the above-shown script. Needless to say, I'm baffled and extremely annoyed. I could simply remove the offending characters (via PHP or Javascript) but it annoys me I can't seem to fix the root of the problem. Please help, thank you.

Daniel Gast
  • 181
  • 13

1 Answers1

3

You could have some kind of invisible BOM mark at the beginning of your file or something else. Always let <? or <?php be the first string of your PHP files and make it a practice NOT to end the entire PHP file with ?> if it's going to be included by another file.

silkfire
  • 24,585
  • 15
  • 82
  • 105
  • Thanks to the contributors above, I had actually just spotted another question on the right-hand side that had my exact problem (and, incidently, solution). I had a few offending spaces just below my closing php tag in engine_core_functions.php. Thank you very much for contributing! http://stackoverflow.com/questions/13892722/why-does-a-unicode-php-file-generate-a-line-break?rq=1 – Daniel Gast Feb 19 '13 at 10:44
  • I had this issue when outputting a file attachment using `fopen('php://output', 'w')` - the UTF-8 BOM was getting preceded by a line-feed character! Per the answer here, having a closing `?>` tag and subsequent empty line was the cause of my problem, though this was not in the PHP file where the problem appeared to be coming from, but rather a controller file higher up the stack. Be sure to check ALL of the PHP files involved in serving the request! – John Rix Sep 26 '16 at 23:46