How do I echo in PHP without carriage returns?

13,481

Solution 1

I have to conclude that a server setting causes PHP to precede and follow all output with an arbitrary number of carriage returns. Of course, this is an error in the config files of the web host I'm working with. It shouldn't be like this.

Solution 2

echo doesn't append a \n in PHP.

You may want to drop the closing ?> though, then you guarantee you won't have any trailing output.

Solution 3

Delete the two carriage returns after the ?> (or remove the ?>)

Solution 4

echo by default does not have carriage return.

Unexpected empty space or lines are mostly caused by the script usage. If your process will be on a single line, then also do use a single line in script side. As for this example; `

On a different topic as a tip, even for more convenient things, you can do so by for simple if/else statements using conditional statements

Consider following example: <?php echo ($foo == "bar") ? "Foo was bar." : "Foo wasn't bar."; ?>

Solution 5

PHP scripts are natively placed in HTML files, and thus white space sensitive. This means any white space surrounding your tags will be visible on screen. As you would want it to be as it could be mid-paragraph!

As the other answers suggest, either clear the new line returns surrounding your tags, or you can cheat remove the ending line breaks by tricking the PHP interpreter - by leaving off the final "?>" tag.

Share:
13,481
Mark
Author by

Mark

Use the contact form on my website if you want to send me a mesage. Economy-x-Talk Software Engineering I wrote a book about LiveCode: Programming LiveCode for the Real Beginner The second edition is available from 23d September 2019.

Updated on June 08, 2022

Comments

  • Mark
    Mark almost 2 years

    Whenever I do an echo in PHP, I get two extra carriage returns. For example:

    <?php
      echo 'abc';
    ?>
    

    yields abc[cr][cr]. If I copy my script to a different server, I even get [cr]abc[cr][cr].

    How do I force PHP to echo only my string 'abc' and not the carriage returns?