In PHP, what does "<<<" represent?

67,761

Solution 1

That's heredoc syntax. You start a heredoc string by putting <<< plus a token of your choice, and terminate it by putting only the token (and nothing else!) on a new line. As a convenience, there is one exception: you are allowed to add a single semicolon after the end delimiter.

Example:

echo <<<HEREDOC
This is a heredoc string.

Newlines and everything else is preserved.
HEREDOC;

Solution 2

This is called a heredoc, and it lets you do a long piece of text that goes over several lines. You can put PHP variables in there and they will replace with the value. The word CHART can be anything. It just needs to be the same to start and stop where the quoted text begins.

You could do the same thing by appending multiple quoted strings, but this is cleaner most of the time for extended documents like this HTML text. There is also something called a nowdoc which is like a single quote string in PHP, but these won't let you use variables inside them.

Solution 3

It is the start of a string that uses the HEREDOC syntax.

A third way to delimit strings is the heredoc syntax: <<<.

After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.

Solution 4

It's PHP's heredoc.

Example:

$sql = <<<MySQL_QUERY
SELECT * 
FROM TAB 
WHERE A = 1 AND B = 2 
MySQL_QUERY;           

Solution 5

It's a heredoc, for long strings that you don't have to worry about quotation marks and whatnot. If you notice the word CHART and then there's a line that says CHART;, that indicates the end of the string.

The important thing to remember when using this format is that whatever string you use to define the end of the string (such as CHART in this case), that word has to appear on a line on its own, followed by a semicolon, and NO characters can occur after the semicolon on the same line, even whitespace, otherwise PHP thinks it's part of the string.

Share:
67,761
Chrish
Author by

Chrish

Updated on January 06, 2022

Comments

  • Chrish
    Chrish over 2 years

    For example:

    $sql = <<<MySQL_QUERY
    
  • Gabriel
    Gabriel over 13 years
    Cool, I didn't know that one... I read the link you sent and honestly, I understand why I could live without that one so far! ;) What would be the advantage of using that for a string?
  • Pekka
    Pekka over 13 years
    @Gabriel it allows you to use both kinds of quotations inside the string without breaking it: <<<END Hello "$name"! How is '$spouse' today? END One huge pain in the ass is, however, that the END marker must not be indented, so Heredoc content usually breaks your code's indentation. It's indeed not really one of PHP's most important features :)
  • Pekka
    Pekka over 13 years
    @YiJiang that was actually because of the german localization! Damn geolocation, always gives me the wrong link even though english is my browser language. Corrected, cheers :)
  • Pekka
    Pekka over 13 years
    @Gabriel you're welcome. @Hugo does have a point, though, there are situations where they do make sense and help make the code more readable. But the missing indenting capability takes a lot away at least in my practice
  • user1464971
    user1464971 almost 12 years
    Oh okay, thanks. instead of searching a long time I figured I would just ask a quick question
  • CodyBugstein
    CodyBugstein about 11 years
    What's the difference between this and a regular string? Why not just do echo "This is a heredoc....."
  • Buttle Butkus
    Buttle Butkus almost 10 years
    @Imray as far as I can remember, you can put single and double quotes into the heredoc as well as variables and everything will work. If you want to use double quotes in your echo statement, you would have to escape them. Still, I stopped using heredoc pretty soon after I started, because I didn't like breaking my indentation patterns for it. Now I form many of my strings as arrays that I later implode("\n",$string_array).
  • SourceOverflow
    SourceOverflow over 6 years
    I'd argue that it is pretty useful if you want to inject different languages. Being able to use newlines without concatenation (which might break code completion etc.) is really nice.
  • laketuna
    laketuna over 2 years
    The <<< example is certainly not clean code. I wouldn't recommend anyone to construct array data using heredoc.