Simple question re \n in strings

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
oldcelt
Forum Newbie
Posts: 7
Joined: Wed Aug 10, 2011 5:29 am

Simple question re \n in strings

Post by oldcelt »

I thought that strings bound by double quotes: e.g.

Code: Select all

print "This is a string \n"; print "Another string"; 
would produce a line feed after printing the first string.

That is not happening so have I missed something?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Simple question re \n in strings

Post by Celauran »

Are you sure it's not happening? Have you checked the HTML source? Bear in mind that line breaks are ignored by HTML, so you wouldn't see it displayed on the page.
oldcelt
Forum Newbie
Posts: 7
Joined: Wed Aug 10, 2011 5:29 am

Re: Simple question re \n in strings

Post by oldcelt »

Thanks for your input. I run the following simple php program on localhost:-

Code: Select all

<?php
$variable = "name";
$literally = 'My $variable will not print!\\n';
print($literally);
$literally = "My $variable will print!\\n";
print($literally);
?>
No line breaks occur - this is the output:-
"My $variable will not print!\nMy name will print!\n"
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Simple question re \n in strings

Post by Celauran »

Well, as I mentioned already, line breaks are ignored by HTML so you wouldn't see them in your browser output, only in the source. Secondly, you're escaping the backslash, meaning that's not a line break character anyway. If you want line breaks in your HTML, use <br>
oldcelt
Forum Newbie
Posts: 7
Joined: Wed Aug 10, 2011 5:29 am

Re: Simple question re \n in strings

Post by oldcelt »

Thanks, that's what I've been doing.

However, just being curious: why put them in the source code if they do nothing when the code is run?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Simple question re \n in strings

Post by Celauran »

PHP is not limited to sending output to the browser. While HTML may disregard line breaks, they're useful in creating mail headers, say, or writing to files.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Simple question re \n in strings

Post by AbraCadaver »

Also, when debugging etc. Do you prefer this:
[text]<div class="topic-actions"><div class="buttons"><div class="reply-icon"><a href="./posting.php?mode=reply&f=1&t=138469" title="Post a reply"><span></span>Post a reply</a></div></div><div class="search-box"><form method="get" id="topic-search" action="./search.php"><fieldset><input class="inputbox search tiny"type="text" name="keywords" id="search_keywords" size="20" value="Search this topic…" onclick="if(this.value=='Search this topic…')this.value='';" onblur="if(this.value=='')this.value='Search this topic…';" /><input class="button2" type="submit" value="Search" /><input type="hidden" name="t" value="138469" /><input type="hidden" name="sf" value="msgonly" /></fieldset></form></div><div class="pagination"><a href="#unread">First unread post</a> &bull; 6 posts &bull; Page <strong>1</strong> of <strong>1</strong></div></div>[/text]
Or this:
[text]<div class="topic-actions">

<div class="buttons">

<div class="reply-icon"><a href="./posting.php?mode=reply&f=1&t=138469" title="Post a reply"><span></span>Post a reply</a></div>

</div>


<div class="search-box">
<form method="get" id="topic-search" action="./search.php">
<fieldset>
<input class="inputbox search tiny" type="text" name="keywords" id="search_keywords" size="20" value="Search this topic…" onclick="if(this.value=='Search this topic…')this.value='';" onblur="if(this.value=='')this.value='Search this topic…';" />
<input class="button2" type="submit" value="Search" />
<input type="hidden" name="t" value="138469" />
<input type="hidden" name="sf" value="msgonly" />

</fieldset>
</form>
</div>

<div class="pagination">
<a href="#unread">First unread post</a> &bull; 6 posts
&bull; Page <strong>1</strong> of <strong>1</strong>
</div>


</div>[/text]
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
oldcelt
Forum Newbie
Posts: 7
Joined: Wed Aug 10, 2011 5:29 am

Re: Simple question re \n in strings

Post by oldcelt »

Ah, yes, of course. Tunnel vision at times - old age doesn't come alone, regrettably! :)
Post Reply