133

I have a php CLI script and cannot get the output to break on new lines. I do

echo 'this is my text\r\n';
echo 'next line';

This gives

this is my text\r\nnext line

Any ideas about how to get the output on different lines?

1
  • 6
    Use double quotes: echo "\n";.
    – Kerrek SB
    Commented Jul 25, 2011 at 15:55

4 Answers 4

308

Use double quotes ".

echo "next line\n";

Additional you can use the system-dependent constant PHP_EOL

echo "this is my text" . PHP_EOL;
5
  • 44
    The use of PHP_EOL should be the preferred method of adding new lines. Commented Feb 15, 2012 at 14:35
  • 3
    @AutomaticPixel For platform compatibility yes, for inter-platform compatibility you should use \n instead.
    – KingCrunch
    Commented Aug 3, 2012 at 20:44
  • 2
    it was helpful and real quick solution :) tnx for sharing Commented Jul 2, 2013 at 12:47
  • 3
    @KingCrunch what you mean by "inter-platform compatibility"?
    – edigu
    Commented Sep 12, 2014 at 6:36
  • 2
    @foozy For example you generate a something on linux but want to read it on a Mac-system, or Win-system. If you don't know the target system, or it can change (--> somebody forwards a mail with a generated file as attachment), use \n
    – KingCrunch
    Commented Sep 12, 2014 at 12:15
18

Use double quotes instead. ".

16

Escape sequences are only parsed when inside double quotes, not single quotes.

http://php.net/manual/en/language.types.string.php

12

Better not to concatenate anything in PHP, because it may lead to unexpected results, instead use a comma:

echo 'Text with new line' , PHP_EOL;

This will be faster too by the way: not concatenating and avoiding parsed double quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.