www.webdeveloper.com
Results 1 to 3 of 3

Thread: What does '<<<END ... END' do?

  1. #1
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    700

    What does '<<<END ... END' do?

    I was looking through some scripts over the weekend and came across a structure that I'm not familiar with...

    PHP Code:
    $variable = <<<END
    <a href="http://www.something.tld">Some HTML here</a>
    END; 
    I'm guessing it allows one to assign HTML to a variable, including quotes, without the need to use the backslash. Is that correct?

    If so, then are there any restrictions or pitfalls?
    If not, then what does it do?
    Can anyone point me to a reference page for the structure?

    Thanks
    CTB
    Oh Lord, please help me be the person my dog thinks I am.

  2. #2
    Join Date
    Jun 2007
    Posts
    348
    From the manual:
    Heredoc

    Another way to delimit strings is by using heredoc syntax ("<<<"). One should provide an identifier after <<<, then the string, and then the same identifier to close the quotation.

    The closing identifier must begin in the first column of the line. Also, the identifier used must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.

    Warning:

    It is very important to note that the line with the closing identifier contains no other characters, except possibly a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs after or before the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by your operating system. This is \r on Macintosh for example.

    If this rule is broken and the closing identifier is not "clean" then it's not considered to be a closing identifier and PHP will continue looking for one. If in this case a proper closing identifier is not found then a parse error will result with the line number being at the end of the script.
    Heredoc text behaves just like a double-quoted string, without the double-quotes. This means that you do not need to escape quotes in your here docs, but you can still use the escape codes listed above. Variables are expanded, but the same care must be taken when expressing complex variables inside a here doc as with strings.

  3. #3
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    700
    Brilliant.
    Thanks so much.
    Oh Lord, please help me be the person my dog thinks I am.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles