Mark
Mark

Reputation: 2435

How do I echo in PHP without carriage returns?

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?

Upvotes: 9

Views: 16500

Answers (5)

Mark
Mark

Reputation: 2435

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.

Upvotes: 1

Dan Belden
Dan Belden

Reputation: 1217

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.

Upvotes: 1

ASertacAkkaya
ASertacAkkaya

Reputation: 661

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."; ?>

Upvotes: 1

alex
alex

Reputation: 490153

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.

Upvotes: 10

Explosion Pills
Explosion Pills

Reputation: 191729

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

Upvotes: 4

Related Questions