We help IT Professionals succeed at work.

Inserting line returns in PHP echo statements

steva
steva asked
on
477 Views
Last Modified: 2012-02-11
I'm having trouble getting echo to echo a CRLF.  I have a loop below, for example, and each time through I'd like to display the index value.

$eol = "\n\r\t";
for($i=1; $i<=$maxorderID; $i++) {

	echo "$eol pulling record $i";

	      etc.

Open in new window


I've also tried putting the \n\r\t directly in the echo.

Could someone point out the way to get the PHP echo to start on a new line?

Thanks.
Comment
Watch Question

Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
"\n\r\t" will only work in a plain text format.  In a web page, you need "<br />".

Author

Commented:
But it seems that this is a plain text format.  It's all between <?php    ?> and there's no <body>, no HTML, etc.  It's just a php script.  If I put a <br /> it just gets displayed as  "<br />".

Commented:
Where is the output displayed? On browser or command line console or else?
If you are trying to echo a new line, you can try PHP_EOL constant.

echo PHP_EOL . "\t .... blah blah blah";

Author

Commented:
The output  appears in the browser when I put www.mysite/script.php into the address field.

I tried the
                                       echo PHP_EOL . "Some text";

but it didn't give me a new line.
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
I've tried echoing "<br /> and it just displays "<br />".  

What does work is <p>, even without a </p>.  For some reason that generates a new line.
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
Displaying plain text (or anything else) in a browser without supplying a correct Content-Type is undefined and is not a 'legitimate' format.  If you are echoing from PHP in a 'file://' instead of thru a server with 'http:', that doesn't work either.  When loading local files in a browser, the browser needs to guess at the type from the file extension.

"Content-Type: text/html; charset=ISO-8859-1" is an example that is frequently used with web pages that generate HTML.

"Content-Type: text/plain;" would be a plain text page sent from the server.  If you served a file with this header, it would be treated as plain text.  Since you're using PHP, it is probably being considered HTML although there is the possibility that there isn't any header being sent at all.

Author

Commented:
I am echoing from a PHP file that is being pulled into the browser by placing www.mysite/myfile.php in the browser address bar.  I am not explicitly sending any headers in the php file.

Are you saying that I should explicitly do a PHP  header()  call first off just below the <?php  to tell the browser what is coming, and if I choose the right parameter for header() the PHP echoes will all act sane in the browser so that < br/> becomes a line feed, and maybe even \n will work?
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
You should do explicitly put PHP header() to check if it is working. If it works, you have to check your web server if it is misconfigured.
Most Valuable Expert 2011
Author of the Year 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Most Valuable Expert 2011
Author of the Year 2014

Commented:
I can never find where the PHP constants are documented, but PHP_EOL is a context-aware end of line character.  That is to say, it will generate the correct CRLF or CR or LF for whatever operating system you are running, and it will be portable if you move your script between Windows and Unix.  It is usually used to make generated HTML easier to read in the "view source" output from the browser.  But it can also be used for shortcut output formatting if you have set <pre> in the browser output stream.
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Thanks, Dave.  I am going to bookmark that!

Sidebar note: My son was just admitted to Johns Hopkins University, so it looks like I am in the harness for another four years ;-)

All the best, ~Ray

Author

Commented:
Thanks.  I'll play with all of these.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.