Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Jan
    Jan is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Finland
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to do line-breaks in PHP?

    Hey all!

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

    PHP Code:
    <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

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    626
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You would use "\n" to force a line feed like this:
    print "some content \n";

  • #3
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Also, to do a tab, use \t

  • #4
    Jan
    Jan is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Finland
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you can escape the double quotes with \, like
    PHP Code:
    print("<div id=\"blablabla\"> \n") ; 

  • #6
    Jan
    Jan is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Finland
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks everybody for your replies! Got it all to work just fine


    ~Jan

  • #7
    New Coder
    Join Date
    Dec 2003
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by fimi; 12-15-2003 at 09:17 PM.

  • #8
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    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

  • #9
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    I've seen it done with single quoted string before, but I can't remember how it was done, it was something weird like

    PHP Code:
    $str 'this is a line '.".\n."
    If I can find it again, I'll post it, the above doesn't look right though

  • #10
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •