140

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?

3
  • If anyone is interested in a solution that works for both browser and terminal (cli, command line): stackoverflow.com/a/9665253/1923625
    – Omar Tariq
    Commented Mar 20, 2017 at 18:03
  • 1
    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. Commented Jul 28, 2020 at 13:32
  • Agreeing with @Victor Schroder ... except go the other way. \n is so prevalent that you will find yourself switching constantly. Use double quotes unless you need single quotes for some reason. Escaping escape sequences and escaping variable substitution should be rare enough. Commented Aug 21 at 17:23

12 Answers 12

163

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

echo 'Hello, world!' . "\xA";
8
  • 1
    Is there any alternative to this? I need to use it in a preg_replace that I cannot use double quotes in. Commented Apr 18, 2012 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
    Commented May 21, 2013 at 9:35
  • 3
    @hakre Surely, reading the manual could have helped, but thankfully, StackOverflow is the place to conveniently look for exactly these answers ;) Commented Jun 6, 2014 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
    Commented Jun 6, 2014 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. Commented May 29, 2015 at 9:36
127
echo 'hollow world' . PHP_EOL;

Use the constant PHP_EOL then it is OS independent too.

1
  • 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
    Commented May 9, 2016 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!';
2
  • 4
    Unfortunately its more for terminal purposes.. thanks for your response though.
    – Matt
    Commented Mar 28, 2010 at 5:19
  • Try this in a text-only email ;)
    – Avatar
    Commented Sep 9, 2018 at 6:19
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.

4
  • 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
    Commented Mar 31, 2010 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. Commented May 8, 2013 at 6: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;'; Commented May 12, 2016 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
    Commented Feb 25, 2020 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.

1
  • 1
    This seems like the prettiest, most lint friendly, portable solution IMO.
    – daraul
    Commented Feb 21, 2020 at 11:13
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';
1
  • 1
    That's compared to the other answers the easiest solution.
    – Avatar
    Commented Sep 9, 2018 at 6:20
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.

6
  • 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
    Commented Oct 9, 2011 at 0:57
  • The best way i know for doing what you want: echo 'foo', "\n";
    – linuxdev
    Commented Oct 9, 2011 at 0:59
  • 3
    The fast way to do it is echo $var + "\n";
    – Hengjie
    Commented Nov 28, 2013 at 13:14
  • 2
    @Hengjie : You probably mean echo $var . "\n";. Commented May 18, 2016 at 8: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 ? Commented May 18, 2016 at 8:44
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.

1
  • 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
    Commented Mar 28, 2010 at 7:47
3

You can use this:

echo 'Hello World' . "\n";
3

This worked well for me:

print_r('Hello world'.PHP_EOL);
0
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

3
  • 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. Commented Mar 28, 2010 at 5: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. Commented Mar 28, 2010 at 6: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. Commented Jul 28, 2020 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>';

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.