I’m trying to implement a simple service in PHP. The service needs to return strings to certain requests. However, when I try to echo the string, PHP somehow adds \r\n
to the beginning of the string. I am using echo
to output the response.
I tried to echo one space character:
$test = ' ';
echo $test;
and in the response I still got '\r\n'
.
I have tried header('Content-type: text');
and $string = preg_replace("\r\n", "", $string);
, but I’m still getting the new line in the response.
I’m new to PHP, so if this is some kind of concept, could someone provide me with pointers to information where I can read about it?
It's possible this character is in your sourcecode somewhere, for instance at the end of one your scripts, like:
?>\r\n
.A best practice is to never include the final
?>
in each file to avoid accidental output.This recommendation is in the PSR-2 Coding Style Guide.
It's also in the manual.