Page 1 of 1
Simple question re \n in strings
Posted: Mon Sep 30, 2013 6:07 am
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?
Re: Simple question re \n in strings
Posted: Mon Sep 30, 2013 6:37 am
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.
Re: Simple question re \n in strings
Posted: Wed Oct 02, 2013 3:47 am
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"
Re: Simple question re \n in strings
Posted: Wed Oct 02, 2013 6:07 am
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>
Re: Simple question re \n in strings
Posted: Wed Oct 02, 2013 7:06 am
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?
Re: Simple question re \n in strings
Posted: Wed Oct 02, 2013 7:32 am
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.
Re: Simple question re \n in strings
Posted: Wed Oct 02, 2013 9:53 am
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> • 6 posts • 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> • 6 posts
• Page <strong>1</strong> of <strong>1</strong>
</div>
</div>[/text]
Re: Simple question re \n in strings
Posted: Thu Oct 03, 2013 4:16 am
by oldcelt
Ah, yes, of course. Tunnel vision at times - old age doesn't come alone, regrettably!
