Newline using single quotes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Newline using single quotes

Post by socket1 »

I have some php code that wont return a newline when its between single quotes ex)

Code: Select all

 
$string1 = 'hello hello hello <br /> \n hello hello hello';
$string2 = "hello hello hello <br /> \n hello hello hello";
 
echo $string1;
echo '<br />';
echo $string2;
 


String1 does not work, it will not return a newline after the <br /> tag, string2 works fine.
I want to use all single quotes since a helpful person pointed out in a post before that using single quotes is better because when you use double quotes in your HTML inside a variable or such you do not have to put \ the escape character before all of them.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Newline using single quotes

Post by Eran »

New line characters (\n) can only be outputted inside double quotes. Just use concatenation for adding your linebreaks:

Code: Select all

$string = '<element attr="value"></element>' . "\n";
Post Reply