2
  • The code is in double quotes and still not working.
  • I already read this post
  • I'm using Atom and am on localhost (if that makes any difference)
  • I redownloaded Atom (in case there was something going on with the settings) and that didn't help

Here's the code:

<?php
$firstName = 'David';
$lastName = "Powers";
$title = '"The Hitchhiker\'s Guide to the Galaxy"';
$author = 'Douglas Adams';
$answer = 42;
$newLines = "\r\n\r\n";

$fullName = "$firstName $lastName |";
$book = "$title by $author";

$message = "Name: $fullName $newLines";
$message .= "Book: $book \r\n\r\n";
$message .= "Answer: $answer";

echo $message;
echo "Line 1\nLine 2";

Output is all one line, but when I view source the new lines are working

Name: David Powers | Book: "The Hitchhiker's Guide to the Galaxy" by Douglas Adams Answer: 42Line 1 Line 2
Community
  • 1
  • 1
Padawan
  • 724
  • 1
  • 8
  • 20
  • 3
    HTML doesn't recognize newlines, you'll need to use `
    ` to print newlines. You can use `nl2br()` function to do this, like `echo nl2br($message);`
    – Qirel Jan 04 '17 at 23:24
  • Are you running the code and viewing the output on the command line or in your browser? – dave Jan 04 '17 at 23:25
  • http://stackoverflow.com/a/588362/1220930 – Timurib Jan 04 '17 at 23:25
  • @dave He mentioned "when I view source". Must be the browser – Barmar Jan 04 '17 at 23:25
  • @Barmar good pickup - I missed that detail. – dave Jan 04 '17 at 23:26
  • 1
    capture all that in an output buffer, (and/or) write it to a file (directly) and those line breaks will be there alright. ;-) Ok, it (probably/most likely) doesn't answer the question directly, but am technically correct on this. However, if the intention is to write to a file, well... the same thing applies. – Funk Forty Niner Jan 04 '17 at 23:39
  • actually that link you posted that you said you already read; has the answer in there, all of them actually and is a duplicate. Most particularly this answer in there http://stackoverflow.com/a/11523781/1415724 and http://stackoverflow.com/a/15133705/1415724 - so what do you want to do, output to screen or write to a file? – Funk Forty Niner Jan 04 '17 at 23:51

3 Answers3

5

This is the first thing you will learn if you are learning even from a PHP 5 For Dummies book. HTML doesn't respect new line or tab or multiple space characters. You have to use <br /> for new lines.

preview
* Sourced from PHP 5 For Dummies by Janet Valade.

Change your code to:

<?php
$firstName = 'David';
$lastName = "Powers";
$title = '"The Hitchhiker\'s Guide to the Galaxy"';
$author = 'Douglas Adams';
$answer = 42;
$newLines = "<br /><br />";

$fullName = "$firstName $lastName |";
$book = "$title by $author";

$message = "Name: $fullName $newLines";
$message .= "Book: $book <br /><br />";
$message .= "Answer: $answer";

echo $message;
echo "Line 1<br />Line 2";

If you are just opting for a text based layout, you can set the header to the browser to respect it as just a text file. For that, you need:

header("Content-type: text/plain");

This will render without any HTML.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • I see. I got this straight out of a Lynda course on Intro PHP. I went and listened to the course again after reading everyone's comments (wondering why it would be in the course if you can't use it to display in browser) and realized I missed his comment, "Browsers don't display carriage returns and new line characters". Thanks, your input helps. – Padawan Jan 04 '17 at 23:57
1

By default, when a PHP script is run, the content type of the result is set to text/html. When browsers render HTML, newlines are not normally respected, they're treated like any other whitespace.

If you want all your output formatting to stay the same, and you're not sending HTML, tell the browser that you're sending plain text. Put this at the beginning of the code:

header("Content-type: text/plain");
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Ok, good to know. Thank you. – Padawan Jan 04 '17 at 23:58
  • Question: ...why do they even bother to teach it (while using a browser for output in their examples) if it's not working anywhere but in the source code? I just wasted 2 hours trying to figure this out, then went back to the Lynda video and relistened; he basically said the same thing, "Browsers don't display new line characters". DOH! – Padawan Jan 05 '17 at 00:00
  • I don't know who "they" is. – Barmar Jan 05 '17 at 00:03
  • There are lots of bad tutorials. When they're teaching one concept, they often tend to ignore everything else. – Barmar Jan 05 '17 at 00:10
1

As well Praveen Kumar mention how to print new lines in echo.

But if you still want to use escape sequences then use print_f You can use other escape sequences like \t,\n in printf.

<?php
$firstName = 'David';
$lastName = "Powers";
$title = '"The Hitchhiker\'s Guide to the Galaxy"';
$author = 'Douglas Adams';
$answer = 42;
$newLines = "\r\n\r\n";

$fullName = "$firstName $lastName |";
$book = "$title by $author";

$message = "Name: $fullName $newLines";
$message .= "Book: $book \r\n\r\n";
$message .= "Answer: $answer";

printf ($message);
printf ("Line 1\nLine 2");
Saad Suri
  • 1,352
  • 1
  • 14
  • 26