138

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?

hakre
  • 193,403
  • 52
  • 435
  • 836
Matt
  • 22,224
  • 25
  • 80
  • 116
  • If anyone is interested in a solution that works for both browser and terminal (cli, command line): http://stackoverflow.com/a/9665253/1923625 – Omar Tariq Mar 20 '17 at 18:03
  • There's honestly no reason at all to seek single quotes _at all costs_. Both syntaxes serve to a reason. If you want interpolated variables or escaped sequences, you are _supposed_ to use double quotes *intentionally*. A good rule to follow is: always use single quotes, _unless_ you want the double quotes _for a good reason_. That's consistency my friends. – Victor Schröder Jul 28 '20 at 13:32

12 Answers12

158

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

echo 'Hello, world!' . "\xA";
hakre
  • 193,403
  • 52
  • 435
  • 836
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • yessir. He answered it in under 10 min. I had to wait till then to accept, then I forgot about it haha. – Matt Mar 28 '10 at 07:46
  • 1
    Is there any alternative to this? I need to use it in a preg_replace that I cannot use double quotes in. – Alex Hadley Apr 18 '12 at 11:35
  • 1
    That's not the right reasoning. It's no because in PHP you can not express a new-line character in single quotes. And that's it. Reading the manual could have helped :) – hakre May 21 '13 at 09:35
  • 3
    @hakre Surely, reading the manual could have helped, but thankfully, StackOverflow is the place to conveniently look for exactly these answers ;) – Frederik Krautwald Jun 06 '14 at 12:25
  • ACtually I have to correct myself, with singlequotes you can enter chr(10) or chr(13) or a combination by typing exactly that in. might not be that readable though ... :) – hakre Jun 06 '14 at 14:28
  • @Ignacio Vazquez-Abrams please let me know what is wrong with my print statement shown below: printf ( " %s %s %s \n", $row[0], $row[1], $row[2]); After three values of row0, row1, row2. Its not going to next line, stays in the same line. – keshav kowshik May 29 '15 at 09:36
  • @Keshav1234: Other than the HTML layout engine folding whitespace? – Ignacio Vazquez-Abrams May 29 '15 at 12:08
  • @IgnacioVazquez-Abrams I did not get what you are asking. I am getting a space if I give a space between the last%s and \n, if i don give a space there I wont get a space. – keshav kowshik May 29 '15 at 12:12
  • The only answer that worked on `repl.it`. `
    `, `'\n`, `'\r\n'` didn't work on the repl.
    – SherylHohman Jan 05 '19 at 06:46
126
echo 'hollow world' . PHP_EOL;

Use the constant PHP_EOL then it is OS independent too.

Cups
  • 6,901
  • 3
  • 26
  • 30
  • 5
    You should usually not use PHP_EOL as this makes your code dependent on the system it is running on. E.g. your Linux server is communicating with another server and then your server gets replaced by Windows... Not good. – Andreas May 09 '16 at 18:12
30

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

echo 'Will print a newline<br/>';
echo 'But this wont!';
Anthony Forloney
  • 90,123
  • 14
  • 117
  • 115
23

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.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
leepowers
  • 37,828
  • 23
  • 98
  • 129
  • It's not a fear.. you just get better performance when you use single quotes, so I try to use them more often then not.. but yah, this isn't really a great solution for this problem, its more trouble than its worth. Thanks. – Matt Mar 31 '10 at 13:03
  • 5
    I haven't tested it but I would bet that using double quotes is faster than using `printf` or string catenation in general. – Mikko Rantalainen May 08 '13 at 06:10
  • It's not always irrational, double-quotes can be problematic, depending on what you want to do. `echo 'For example, I want this to spit out php code.\necho $this->stuff;';` – David Baucum May 12 '16 at 16:02
  • @DavidBaucum, I know this is an old thread but just for reference for yourself and anyone else who stumbles across this, `echo "For example, I want this to spit out php code.\necho \$test;";` - as you can see you can escape the variables, which resolves the problem you describe with double quotes. – Josh Wood Feb 25 '20 at 14:45
12

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.

NewSites
  • 1,402
  • 2
  • 11
  • 26
9

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';
Alana Storm
  • 164,128
  • 91
  • 395
  • 599
8

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.

Nikola K.
  • 7,093
  • 13
  • 31
  • 39
linuxdev
  • 89
  • 1
  • 1
  • I applaude you for trying to search the best way of doing this instead of using the double quotes solution A.K.A The lazy-developer-solution. – linuxdev Oct 09 '11 at 00:57
  • The best way i know for doing what you want: echo 'foo', "\n"; – linuxdev Oct 09 '11 at 00:59
  • 3
    The fast way to do it is `echo $var + "\n";` – Hengjie Nov 28 '13 at 13:14
  • 2
    @Hengjie : You probably mean `echo $var . "\n";`. – Skippy le Grand Gourou May 18 '16 at 08:37
  • @linuxdev : I don't see the point of this answer. Since the output of 1 is not the same as the output of 2 anyway (even assuming `$var='$var'`…), aren't you comparing apples with oranges ? – Skippy le Grand Gourou May 18 '16 at 08:44
  • Very, very bad, terrible advice. There's absolutely no meaningful difference of using single or double quotes, when it comes to performance. Concatenating would also have some hypothetical overhead, all this is pointless nano-optimization. Use double quotes when you have a good reason to do so, like interpolating variables or printing an escaped sequence. Otherwise, use single quotes to _demonstrate_ your intent of not performing any of these operations. – Victor Schröder May 21 '21 at 01:13
3

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.

Duniyadnd
  • 4,013
  • 1
  • 22
  • 29
  • 1
    Ah yes... but I still HATE the fact that VARIABLE at the end breaks all indentation in your code... I have ran into so many (stupid) errors using HEREDOC syntax – Matt Mar 28 '10 at 07:47
3

You can use this:

echo 'Hello World' . "\n";
Rob
  • 26,989
  • 16
  • 82
  • 98
Cisco Test
  • 41
  • 2
3

This worked well for me:

print_r('Hello world'.PHP_EOL);
Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130
0

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

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • There is a slight performance increase to using single quotes. Also, it seems like a good practice to explicitly define when you want variables to be replaced. – Jackson Miller Mar 28 '10 at 05:42
  • 4
    @Jackson there is **no** performance increase. By any means. Forget these childish rumors. And no, do talk not of variables, but of special sequences. If you want any sequence, a variable or a newline to be expanded - use double quotes, **on it's purpose**. And OP's refusing to use it on it's purpose is nonsense. – Your Common Sense Mar 28 '10 at 06:03
  • There's absolutely no reason to not use double quotes when you want to interpolate a variable inside a string. Same goes with an escaped `/n` for line breaks. It's easier, cleaner, more readable and has _zero_ performance penalty. – Victor Schröder Jul 28 '20 at 13:28
0

in case you have a variable :

$your_var = 'declare your var';
echo 'i want to show my var here'.$your_var.'<br>';
Braham Youssef
  • 479
  • 6
  • 6