2

I've tried every method to output a newline in PHP. Why doesn't the following work? :

<?php

$foo = 'bar'; 
echo "Hello \n $foo!";

?>

This should output a newline between hello and bar but it isn't.

I also tried \r\n instead of \n

user2463517
  • 197
  • 1
  • 5
  • 14
  • 3
    A newline doesn't mean anything ***in HTML***. – deceze Jun 07 '13 at 19:45
  • 1
    @deceze Technically it should be the same as a space. – NoBugs Jun 07 '13 at 19:46
  • 2
    @NoBugs OK, "not anything" is not quite correct... :) – deceze Jun 07 '13 at 19:46
  • Guys, I understand this is a stupid question, obviously the asker didn't try googling it. But down-vote? Why? Code + question is a solid question. Doesn't deserve a down-vote. Be nice, answer the question with the correct answer and "Try googling next time" – Dummy Code Jun 07 '13 at 19:50
  • 1
    I think people are encouraged to downvote because when you hover over the downvote button it states *"This question does not show any research effort; it is unclear or not useful"* – BLaZuRE Jun 07 '13 at 19:52
  • 2
    @Henry SO is (at least partly) supposed to be a database of programming problems *which are not readily answered everywhere else.* This problem here is one of the first things one should come across following any tutorial for HTML. We do not want this to clog up search results when you search for a real problem. Hence, downvote. – deceze Jun 07 '13 at 19:54
  • @deceze Good point. I guess it is just a personal thing. I was once in this guys shoes and I wish more SO users just replied with a quick comment like, try googling or something before asking on SO instead of dishing out down-votes to questions that are legitimate questions without the user understanding what he did wrong. – Dummy Code Jun 07 '13 at 19:59
  • @HenryHarris or `she`. Can't leave the ladies out ;-) – Funk Forty Niner Jun 07 '13 at 20:00
  • @Fred Ha. Sorry for the .001% of stack users left out. ;) – Dummy Code Jun 07 '13 at 20:01
  • @HenryHarris there are actual `stats`? lol - one never knows one's `gender` if the obvious starts off like Bob, Larry, Curly or Moe. It's those `user*****` that you have to start wondering about! – Funk Forty Niner Jun 07 '13 at 20:06
  • @fred haha. just going by odds `he` is a safe bet. ;) – Dummy Code Jun 07 '13 at 20:07
  • @user24... BTW, welcome to SO. Just, please, show some more initiative the next time. Following a decent basic tutorial or book for the topic at hand is the least one is expected to do before dropping a question. – deceze Jun 07 '13 at 20:10
  • @HenryHarris Indeed ;-) However, boats are all `SHEs`. Now that's completely off-topic, but just as much fun! cheers – Funk Forty Niner Jun 07 '13 at 20:10
  • Do you have PHP 7.0 installed and are on a Windows machine? Use "
    ";
    – Mark Mar 02 '21 at 02:38

6 Answers6

11

If you using it as command line script this would work. I would use PHP_EOL for this since it chooses the right line break for the OS.

However if you are working with HTML (viewing the result in browser for example) you have to use the HTML way of linebreaks which is: <br />

DAG
  • 6,710
  • 4
  • 39
  • 63
7

To output a new line in HTML you need to use HTML's representation of a new line which is <br/>

php has a function for you that converts all natural new lines to HTML new lines > nl2br()

Then your code should look like

<?php

$foo = 'bar'; 
echo nl2br("Hello \n $foo!");

?>
php_nub_qq
  • 15,199
  • 21
  • 74
  • 144
6

Use <br> when inserting line break in html

NoBugs
  • 9,310
  • 13
  • 80
  • 146
3

To output a new line visually (in a browser), you need HTML:

echo "Hello\n<br />$foo!";

\n is a system line feed.

Kermit
  • 33,827
  • 13
  • 85
  • 121
  • 1
    This is only when opening a website using PHP. PHP can be executed in console. For console, his code will run just fine. For webpages, you code will run ok. – Alejandro Iván Jun 07 '13 at 19:46
  • To clarify, to output a new line using a program that renders HTML (i.e. a browser), you need HTML. If you're outputting a new line visually, like in a file or elsewhere, you might want a carriage return and line feed. – BLaZuRE Jun 07 '13 at 19:46
  • @AlejandroIván Using 99% certainty in guessing they are outputting this in a browser. – Kermit Jun 07 '13 at 19:46
  • Yeah, I guessed the same. I commented only as reference ;) – Alejandro Iván Jun 07 '13 at 19:47
  • On command line this ur code return 2x new line. Better to use on `\n` or `
    ` or best `nl2br($str . PHP_EOL);`
    – Ivan Jan 12 '15 at 14:54
1

Try this:

<?php

$foo = 'bar'; 
echo "Hello <br> $foo!";,

?>
Lenin
  • 570
  • 16
  • 36
0

if you view the source you will see it actually DOES have a new line in it, But white space doesnt matter in HTML (what the browser displaying the output is expecting) but adding a <br /> to your statement will produce a new line because this is the way browsers read new lines and display them.

exussum
  • 18,275
  • 8
  • 32
  • 65