PDA

View Full Version : How to do line-breaks in PHP?



Jan
12-12-2003, 11:00 AM
Hey all!

I have this problem. I've made a website using some PHP:



<div id="wrapper">
<?php include("header.php") ?>
<div id="middle">
Here's the actual content
</div>
<?php include("footer.php") ?>
</div>


I have several div's in the header a footer files, and these are tabbed. Then, when I upload the files and check the sourcecode, they don't have line-breaks where they should. I know this isn't that serious, but I want my code to look okay! So, is there a way of putting line-breaks in PHP? Thanks for any help!


~Jan

requestcode
12-12-2003, 01:34 PM
You would use "\n" to force a line feed like this:
print "some content \n";

Nightfire
12-12-2003, 02:50 PM
Also, to do a tab, use \t

Jan
12-15-2003, 10:08 AM
Thanks for your replies! One question though, how would I make this work, if I'd want it to be XHTML 1.0 Strict compliant? I mean, I can't use "" for the id's (eg. <div id="blablabla">) if it's inside print(""). Is there a solution for this?


~Jan

raf
12-15-2003, 10:29 AM
you can escape the double quotes with \, like


print("<div id=\"blablabla\"> \n") ;

Jan
12-15-2003, 02:29 PM
Thanks everybody for your replies! Got it all to work just fine :)


~Jan

fimi
12-15-2003, 08:14 PM
or you can also use
$var = ' " content" ' ;
it would print "content".
i use that when i have to print sth like that...adding \ makes me sad.:thumbsup:

raf
12-15-2003, 09:23 PM
Originally posted by fimi
or you can also use
$var = ' " content" ' ;
it would print "content".
i use that when i have to print sth like that...adding \ makes me sad.:thumbsup:

It might be just me, but i've never managed to get a linebreak when i have
'whatever"bb"bbdsd"nn" \n'
I always prints the \n as characters
try
print '<a href="content.php">test</a>\n' ;
and then
print "<a href=\"content.php\">test</a>\n" ;

and you'll see that in the form you suggest, the \n is printed as actual text and is not parsed to generate a linebreak.

But then again, it's maybe just me that doesn't know how to do it with the single quoted strings.
I normally also use single quotes for printing xhtml

Nightfire
12-15-2003, 09:27 PM
I've seen it done with single quoted string before, but I can't remember how it was done, it was something weird like



$str = 'this is a line '.".\n.";


If I can find it again, I'll post it, the above doesn't look right though

raf
12-15-2003, 09:35 PM
echo 'this is a line '."\n";

would do the trick (since you'de then just use a double quotes and concatinate it with the single quoted string.
Might be easier when printing xhtml the the backslashing.

Thanks for pointing me in the right direction.