0

Both these inputs get outputted as a single line.

I'm doing it just as my book shows. According to my book, these formats should respect all the whitespace and linebreaks without having to do the \n crap.

<?php 
$author = "Alfred E Newman"; 

echo <<<_END 
This is a Headline 

This is the first line. 
This is the second. 
- Written by $author. 
_END; 
?> 

<?php 
$author = "Alfred E Newman"; 
$text = "This is a Headline 

This is the first line. 
This is the second. 
Written by $author."; 
echo $text 
?> 

EDIT here' i'll quote my book on what it says this block of code should do " PHP also offers a multiline sequence using the <<< operator, commonly referred to as a here-doccument or heredoc for short. This is a way of specifying a string literal, preserving the line breaks and other whitespace (including indentation) in the text. Its use can be seen in Example 3-8." Eg.3-8 would be the first piece of code I posted on here. The book I'm using is, "Learning PHP, MySQL, JavaScript, and CSS" by Robin Nixon

ANOTHER EDIT Okay this is weird, I copy and pasted the Example #2 for the heredoc example on from the PHP manual into my PHP IDE and got the same result when the output should obviously be different "http://www.php.net/manual/en/language.types.string.php"

perhaps it's my IDE? I am running WAMP and everything seemed to be running fine until now...

EDIT AGAIN zomfg i hate when this happens, later on in the book he says it's normal because HTML formatting rules take over and white space is supprpessed" My bad guys. TBH, I don't see the point of it being only visible in the source page but not being formatted with the new lines. What good use is that when you can just open the original file and see it for yourself with the

KingHarambe
  • 111
  • 6
  • 2
    What book are you using? – summea Mar 31 '14 at 01:40
  • 1
    Line breaks only work if you are view it as text. If you are viewing the output in a browser, then you need to use
    instead of newline chars
    – bumperbox Mar 31 '14 at 01:40
  • It's also worth clarifying, it's not your PHP which isn't outputting on new lines, it's your HTML that isn't outputting on new lines. You can make a new HTML document with new lines in the code, and you won't see new lines in the webpage. – Dean Rather Mar 31 '14 at 23:54

4 Answers4

3

You can put it in a <pre> tag, otherwise you need <br> tags for HTML to recognise newlines.

echo "<pre>$text</pre>";
Dean Rather
  • 31,756
  • 15
  • 66
  • 72
1

An alternative to adding HTML tags is to present the document as plain text. Add this at the top (before any echo statements)

header('Content-type: text/plain');
Phil
  • 157,677
  • 23
  • 242
  • 245
  • 1
    Despite your qualification of *before any `echo` statements*, eventually everybody needs [How to fix “Headers already sent” error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php). – kojiro Mar 31 '14 at 01:49
0
$text = "This is the first line.<br>This is the second.<br>Written by $author." ;
YouSer
  • 393
  • 3
  • 12
  • Yeah I'm familiar with the
    but that doesn't really answer my question in regards to what the author was trying to get me to accomplish. I did update my post. Thanks
    – KingHarambe Mar 31 '14 at 05:48
0

There's a php function nl2br (New-Line to Break Rule) which essentially inserts a <br> tag at each of your new lines.

This might be another way to prepare your text for HTML output.

Dean Rather
  • 31,756
  • 15
  • 66
  • 72