echoing using <<<OUT and OUT;

Discussion in 'PHP' started by ignas2526, Dec 1, 2008.

  1. #1
    Hello,
    Today i found one interesting code, there some contents was echoing using
    <<<OUT and OUT; as content holders:
          echo <<<OUT
          something here ' " /
    OUT;
    echo "<br>";
    PHP:
    as you see highlighting fails!
    But the thing is what this code work fine!
    I never seen that way to output strings, even google showed nothing: google result
    I want to know will it work on all servers, and will it work witch older versions of php?
     
    ignas2526, Dec 1, 2008 IP
  2. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #2
    yup...
    this is another method for access string process.
    another example
    
    $user = 'foo';
    $email = 'foo@foo.com';
    echo <<<CONT
    Hello {$user}, good morning.<br />
    Your email is {$email}
    CONT;
    PHP:
     
    misbah, Dec 1, 2008 IP
  3. ignas2526

    ignas2526 Peon

    Messages:
    75
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    How i see they not very popular...
     
    ignas2526, Dec 1, 2008 IP
  4. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #4
    maybe yes...
    or maybe not (cause we can't find this query (<<<) on google)
     
    misbah, Dec 1, 2008 IP
  5. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #5
    It's called 'heredoc' syntax. It's useful if you want to throw about strings with lots of quotes in them and so forth, but still want to be able to use a variable here and there.

    http://ca.php.net/types.string
     
    TwistMyArm, Dec 1, 2008 IP
  6. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #6
    Google doesn't play well with symbols at all. Probably my biggest/only complaint about their search function.

    I've never been a fan of Heredoc syntax. Half or more of php programmers you run into don't know about it. It doesn't validate under any IDE that I've ever used, and it makes formatting more difficult than standard echo ' '..' ';.
     
    jestep, Dec 1, 2008 IP
  7. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #7
    Yeah... like jestep said...
    I like standard echo (without heredoc syntax) .
     
    misbah, Dec 1, 2008 IP