Daniel Gast
Daniel Gast

Reputation: 195

Line feed is showing up unexpectedly in PHP

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.

Upvotes: 4

Views: 192

Answers (1)

silkfire
silkfire

Reputation: 25935

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.

Upvotes: 3

Related Questions