Page 1 of 3 123 LastLast
Results 1 to 10 of 25

Thread: basics

  1. #1
    Join Date
    Jan 2007
    Location
    Close to the fridge
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default basics

    Ok I am still getting though the basics here.

    special characters you can use in strings: \n and \t. You
    can use \n to start a new line in a string, as in the following statements:

    So I put in
    $string = “Hello \nWorld”;
    echo $string;
    But the output is still Hello World instead of
    Hello
    World

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Try

    Code:
    $string = "Hello \r\nWorld";
    echo $string;
    Then it should display:

    Hello
    World
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Jan 2007
    Location
    Close to the fridge
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I just tried that and it did not work...
    still displayed Hello World

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    This is because HTML ignores linebreaks.

    If you look at the source, you will see:
    Code:
    Hello 
    World
    but if you want to display a linebreak in HTML, you must use <br> (or, preferably, start a new block-level element; <br> is rarely appropriate).
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jan 2007
    Location
    Close to the fridge
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    That's what I had been doing prior to reading this, so now I am confused as to why would anyone use \n or \t anyways?

    I am sure there is a good reason I just don't know it.

  6. #6
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Quote Originally Posted by Frog Brew View Post
    That's what I had been doing prior to reading this, so now I am confused as to why would anyone use \n or \t anyways?

    I am sure there is a good reason I just don't know it.
    I would think mostly for automated messages (emails) or inserting data to a text file, and stuff like that.

    Added Later: I forgot about HTML ignoring line breaks. Thanks Twey for refreshing my memory on this.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    That's what I had been doing prior to reading this, so now I am confused as to why would anyone use \n or \t anyways?

    I am sure there is a good reason I just don't know it.
    Because PHP outputs to other formats than HTML

    Plus it's a convention still common from C.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Jan 2007
    Location
    Close to the fridge
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ok thanks...am I dislodging memories from wake back when lol..

  9. #9
    Join Date
    Jan 2007
    Location
    Close to the fridge
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Also...is it possible that some things may not work because I reading a book on PHP 5 ? My version on the server is 4.4.4.

    <?php
    $string1 = "Hello";
    $string2 = "World!";
    $stringall = $string1.$string2;
    echo "$stringall";

    ?>

    Notice that no space appears between Hello and World!. That’s because no
    spaces are included in the two strings that are joined. You can add a space
    between the words by joining three strings together — the two variables and
    a string that contains a single space — with the following statement rather
    than the earlier statement:
    $stringall = $string1._ _.$string2;
    I am trying to get measly space between the 2 words and it's not working. I am going by the books intructions.

  10. #10
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    You don't need quotes around the echo. Try this:
    Code:
    $string1 = "Hello";
    $string2 = "World!";
    $space = "&nbsp;";
    $stringall = $string1.$space.$string2;
    echo $stringall;
    If you want multiple spaces...
    Code:
    $string1 = "Hello";
    $string2 = "World!";
    $max = 3; // number of spaces
    for ($i = 0;$i<$max;$i++) {$space.="$nbsp;"};
    $stringall = $string1.$space.$string2;
    echo "$stringall";
    - Mike

Bookmarks

Posting Permissions

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