Print newline in PHP in single quotes

PhpString

Php Problem Overview


I try to use single quotes as much as possible and I've noticed that I can't use \n in single quotes. I know I can just enter a newline literally by pressing return, but that screws up the indentation of my code.

Is there some ASCII character or something that I can type that will produce newline when I'm using single quotes?

Php Solutions


Solution 1 - Php

No, because single-quotes even inhibit hex code replacement.

echo 'Hello, world!' . "\xA";

Solution 2 - Php

echo 'hollow world' . PHP_EOL;

Use the constant PHP_EOL then it is OS independent too.

Solution 3 - Php

If you are echoing to a browser, you can use <br/> with your statement:

echo 'Will print a newline<br/>';
echo 'But this wont!';

Solution 4 - Php

FYI it is possible to get newlines into strings without double quotes:

printf('Please%1$sgive%1$sme%1$snewlines%1$s', PHP_EOL);

Which may be useful If your irrational fear of double quotes knows no bounds. Though I fear this cure may be worse than the disease.

Solution 5 - Php

The only escape sequence you can use in single quotes is for the single quote itself.

$foo = 'That\'s great';

The only way you could insert a new line into a string created with single quotes is to insert a literal newline

$bar = 'That\'s
cheating';

Solution 6 - Php

I wonder why no one added the alternative of using the function chr():

echo 'Hello World!' . chr(10);

or, more efficient if you're going to repeat it a million times:

define('C_NewLine', chr(10));
...
echo 'Hello World!' . C_NewLine;

This avoids the silly-looking notation of concatenating a single- and double-quoted string.

Solution 7 - Php

There IS a difference on using single VS double quotes in PHP

e.g:

  1. echo '$var\n';
  2. echo "$var\n";
  • in 1, PHP will print literally: $var\n
  • in 2, PHP will have to search the location in memory for $var, and return the value in that location, also, it will have to parse the \n as a new line character and print that result

We're in the range of millionths of a second, but there IS a difference in performance. I would recommend you to use single quotes whenever possible, even knowing you won't be able to perceive this performance increase. But I'm a paranoid developer when it comes to performance.

Solution 8 - Php

You may want to consider using <<<

e.g.

<<<VARIABLE
this is some
random text
that I'm typing 
here and I will end it with the 
same word I started it with
VARIABLE

More info at: http://php.net/manual/en/language.types.string.php

Btw - Some Coding environments don't know how to handle the above syntax.

Solution 9 - Php

You can use this:

echo 'Hello World' . "\n";

Solution 10 - Php

This worked well for me:

print_r('Hello world'.PHP_EOL);

Solution 11 - Php

No, according to documentation, PHP recognize no special symbol in single quotes. And there is no single reason to use single quotes as much as possible

Solution 12 - Php

in case you have a variable :

$your_var = 'declare your var';
echo 'i want to show my var here'.$your_var.'<br>';

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
QuestionMattView Question on Stackoverflow
Solution 1 - PhpIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 2 - PhpCupsView Answer on Stackoverflow
Solution 3 - PhpAnthony ForloneyView Answer on Stackoverflow
Solution 4 - PhpleepowersView Answer on Stackoverflow
Solution 5 - PhpAlan StormView Answer on Stackoverflow
Solution 6 - PhpNewSitesView Answer on Stackoverflow
Solution 7 - PhplinuxdevView Answer on Stackoverflow
Solution 8 - PhpDuniyadndView Answer on Stackoverflow
Solution 9 - PhpCisco TestView Answer on Stackoverflow
Solution 10 - PhpMohammad Zaid PathanView Answer on Stackoverflow
Solution 11 - PhpYour Common SenseView Answer on Stackoverflow
Solution 12 - PhpBraham YoussefView Answer on Stackoverflow