tags:

views:

396

answers:

7

Hey all,

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?

Thanks! Matt Mueller

+10  A: 

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

echo 'Hello, world!' . "\n";
Ignacio Vazquez-Abrams
Ah ok.. I was hoping I wouldn't have to resort to that.. Thanks!
Matt
Matt:please accept the answer once you are done.
yessir. He answered it in under 10 min. I had to wait till then to accept, then I forgot about it haha.
Matt
+1  A: 

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
Unfortunately its more for terminal purposes.. thanks for your response though.
Matt
Not a problem, good luck.
Anthony Forloney
+1  A: 

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
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
A: 

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

Col. Shrapnel
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
@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.
Col. Shrapnel
A: 

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';
Alan Storm
+2  A: 

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.

pygorex1
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
+2  A: 

echo 'hollow world' . PHP_EOL ;

Then it is OS independent too.

Cups