SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict Quaint's Avatar
    Join Date
    May 2004
    Location
    Netherlands
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    (Useful?!!) Tip on printing large texts

    Hey,

    I have been using this technique over and over since recently, works perfect to me...

    Say you want to post a large piece of HTML texts (or perhaps and e-mail message) which includes variables, you could do something like this:

    PHP Code:
    echo "Hello" $row['username'] . ", and welcome to my website!<BR>\n";
    echo 
    "How are you doing today (which is: " $date ")?? And how is the weather over there in " $row['country'] . "<BR>\n";
    echo 
    "This text is very lame, but hey, you try to figure out something!<BR>\n";
    echo 
    "The quote for today is: \"" $quotes['dayquote'] . "\"<BR>\n"
    Or this:

    PHP Code:
    echo <<<EOD
    Hello {$row['username']}, and welcome to my website!<BR>
    How are you doing today (which is: 
    {$date})?? And how is the weather over there in {$row['country']} <BR>
    This text is very lame, but hey, you try to figure out something!<BR>
    The quote for today is: "
    {$quotes['dayquote']}"<BR>
    EOD; 
    First of all, notice the curly brackets { } around variables. They identify that there are variables inside meaning PHP can parse them. Normally, PHP can parse variables inside double quotes " ", but it can't parse arrays: $row['username'], you would have to break up the quotes there...

    Then notice how much easier it is to read this other method. Simple text, no quotes, no escaping quotes either...

    Maybe some of you were using this technique already, maybe it isn't any good at all, throw in some thoughts will ya?!

  2. #2
    SitePoint Enthusiast Viper_SB's Avatar
    Join Date
    Aug 2003
    Location
    Canada
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's called HEREDOC it's used for outputting large amount of text within PHP, without having to esacpe anything inside, variables are the only thing you can use in it, so it isn't always as useful as you'd think.

  3. #3
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your second example is almost exclusively the same way I code. It is much easier to read and follow, in my opinion. It's also easier to maintain.

    That said, there's nothing wrong with using echo. It all comes down to personal preference, and possibly coding standards for your shop.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  4. #4
    SitePoint Addict Quaint's Avatar
    Join Date
    May 2004
    Location
    Netherlands
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vinyl-junkie
    That said, there's nothing wrong with using echo. It all comes down to personal preference, and possibly coding standards for your shop.
    Exactly, it does come all down to personal preference! But that of course, is the question.. I know what it is, I know how it works, I know *I* prefer to use it, but to do you?

    Quaint Tech
    - Blog on web development and web technology.


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
  •