New Line on PHP CLI

Php

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

Use double quotes ".

echo "next line\n";

Additional you can use the system-dependent constant PHP_EOL

echo "this is my text" . PHP_EOL;

Solution 2 - Php

Use double quotes instead. ".

Solution 3 - Php

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

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

Solution 4 - Php

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBetaRideView Question on Stackoverflow
Solution 1 - PhpKingCrunchView Answer on Stackoverflow
Solution 2 - PhpDaniel A. WhiteView Answer on Stackoverflow
Solution 3 - PhpDan GrossmanView Answer on Stackoverflow
Solution 4 - PhpemixView Answer on Stackoverflow