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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Posts
    299
    Thanks
    67
    Thanked 8 Times in 8 Posts

    What on earth is the "<<<" notation?

    I've been coding PHP for a while and just now I've come across some new string notation here: http://php.net/manual/en/domdocument.loadxml.php

    For example:

    PHP Code:
    $str = <<<XML
    <?xml version="1.0" encoding="iso-8859-1"?>
    <!DOCTYPE root [
    <!ENTITY nbsp "*">
    ]>
    <div>This&nbsp;is a non-breaking space.</div>
    XML;
    What's with the <<<XML .... XML; notation? How is it different, functionally, to quoting the markup in a string?

    EDIT: Ha ha ha! I've just tried using that notation in some code and it just gives me a syntax error! Works fine with quotes though.
    Last edited by XmisterIS; 04-27-2012 at 10:52 AM.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,864
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    It's the heredoc
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    XmisterIS (04-30-2012)

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,439
    Thanks
    62
    Thanked 537 Times in 524 Posts
    <<< is a heredoc. It's very useful as it allows you to return a whole load of stuff into a string with variables included in it. In the sample code you provided, everything upto the XML; will be stored in the variable called $str.

    There is also the newer nowdoc which works like this:

    $str<<<'XML'

    //stuff

    XML;

    The nowdoc will return anything literally as you see it. If you have any variables in there they will be returned as variables including the $ sign and NOT replaced by their values.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    XmisterIS (04-30-2012)


  •  

    Posting Permissions

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