开发者

how to use <<< tag in php

I am following a book tutorial where the author do like this to display html

eks:

return <<<ADMIN_OPTIONS

<a href="admin.php" class="admin">+ add a new event</a>
<form action="assets/inc/process.inc.php" method="post">
    <div>
        <input type="submit" value="Log Out" class="admin" />
        <input type="hidden" name="token" value="$_SESSION[token]" />
        <input type="hidden" name="action" value="user_logout" />
    </div>
</form>
ADMIN_OPTIO开发者_StackOverflow中文版NS;

I don't quite understand the <<<, from my understanding you can call ADMIN_OPTIONS whatever as long as you end the html with the same words. The thing I really don't understand though is when I write ADMIN_OPTIONS; and all my code goes in comment mode. For example:

The above example works, this does not:

return <<<ADMIN_OPTIONS

<a href="login.php">Log In</a>

ADMIN_OPTIONS; 

but this works:

        return <<<ADMIN_OPTIONS

        <a href="login.php">Log In</a>

ADMIN_OPTIONS;


As others mentioned, this is called heredoc.

You can choose the identifier freely. It's very similar to using double quotes, you can embedd variables with {$var} too. Because of that you also can't use them in any static context, like a default value in a class, consts or define()s. That's why nowdocs were introduced (in PHP 5.3).

If you read the manual entry, you'll quickly find out why your example doesn't work; there must not be any spaces or tabs in front of the ending identifier.

From the manual:

It is very important to note that the line with the closing identifier must contain 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 before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system.


It's Heredoc. Not usually used often.

I think your second example doesn't work because the end identifier line is not "clean". There's a space after the semicolon, whereas the first and third examples are clean.


The syntax you are using is called heredoc, and it is explained in the following section of the PHP manual:

http://php.net/manual/en/language.types.string.php

Basically all it is is an alternative to encapsulating strings within quotes.


These are exactly the same as:

return "

foo bar

";

The difference is that you can specify the termination character sequence yourself, i.e. you're not limited to quotes to start and end your string.

return <<<TERMINATIONSEQUENCE

Because I'm not limited to "quotes",
I can 'freely' use them in "this 'string"'
without worrying about escaping them!

TERMINATIONSEQUENCE;


This is called Heredoc, they behave exactly like strings with double quotes, except without the double quotes.. thus you don't need to escape those.

It also preserves the line breaks and other whitespace in the text.

The ADMIN_OPTIONS is just a identifier to open and close the operation.


You've got a space after the semicolon in the non-working example, that's why PHP doesn't recognize it.


After viewing the source of the OP one can notice that the spacing and tabulation is not identical in the second case to the first & third cases.

Heredoc like mentioned by many requires that your terminator be the only item on the line. You cannot have a tab, or any other white space preceding it.

Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜