This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the use of <<<EOD in PHP?
I know how to use it, I know it's used to mark Heredoc's syntax to easily print multiple lines of string, but what is full name of that EOD acronym (if it's acronym) ?
Only thing I heard is End Of Data, but I'm not sure if it's good.
EOD isn't part of the Heredoc syntax. It's just used in their example.
$foo = <<<JAVASCRIPT
alert('Hello!');
alert('World!');
JAVASCRIPT;
This example would echo the javascript back to the user (or in other words, until the Token JAVASCRIPT is reached).
The heredoc identifier can be chosen at will:
http://php.net/manual/en/language.types.string.php
[T]he identifier 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.
So it might just mean "End Of Data" as well as "Explosive Ordnance Disposal", but it doesn't really matter, it's jsut an identifier, and it could as well have been _Fo0b4R as any other (valid) identifier.
Related
This question already has answers here:
Mixing a PHP variable with a string literal
(5 answers)
Closed 4 months ago.
My actual use case involves filling in javascript variable names with partial id's from php, but to illustrate the issue here's a simple example with html:
$var="ello worl";
echo <<<HTML
H$var d
HTML;
I want the output to be "Hello world" however of course there is a space after the $var variable name so the output is "Hello worl d". If I remove the space, then it changes the variable name.
How do I place text next to the right side of the variable?
I've tried quotes and escaping etc. but to no avail.
You can enclose the variable in curly braces ({ and }) to ensure the PHP interpeter knows which characters are part of the variable name and which are static text.
$var="ello worl";
echo <<<HTML
H{$var}d
HTML;
Demo: https://3v4l.org/TYgEF
Documentation reference: https://www.php.net/manual/en/language.types.string.php#language.types.string.parsing
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to print "that's it!" Exactly as same with double and single quotations in php? Can anyone have solution?
You just have to escape the single quote with a back slash
echo '"that\'s it!"';
I'll post something different because I like them and it think it's often missed or unknown by less experienced programmers.
HereDoc/NowDoc
//HereDoc works like " for variables
echo <<<TXT
"that's it!"
TXT;
The TXT tag can be anything that follows the same rules as variable names (\w+, and must start with Alpha), and the ending one must be on it's own line with "ABSOULTLY" nothing else besides ; (which can be omitted in arrays), nothing else not even whitespaces.
//NowDoc works like ' for variables
echo <<<'TXT'
"that's it!"
TXT;
It's often overlooked, as it's not documented in any really visible place, it's the third example on the string type page on PHP.net. Then after a bunch of scrolling you'll find the Nowdoc part, the only real difference is how they treat variables, as I mentioned.
http://php.net/language.types.string#language.types.string.syntax.heredoc
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.
The closing identifier must begin in the first column of the line. Also, the identifier 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 must contain no other characters, except 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. This is \n on UNIX systems, including macOS. The closing delimiter must also be followed by a newline.
If this rule is broken and the closing identifier is not "clean", it will not be considered a closing identifier, and PHP will continue looking for one. If a proper closing identifier is not found before the end of the current file, a parse error will result at the last line.
This is also common in other languages besides PHP (with minor variations), and it's the cleanest way (for larger amounts of text, think HTML, JavaScript and a mix of those and PHP).
$selector = 'a.foobar';
echo <<<HTML
<script type="text/javascript">
;( function( $, window, document, undefined ) {
"use strict";
$(document).ready(function(){
$('tr.foobar').css('display', 'none');//singe quotes are fine
$("div.foobar").css("display", "none");//double quotes are too
$({$selector}).css('display', "none");//we can even mix them up if we want.
//we can use PHP variable like {$selector}, even these
//comments become comments in the JS.
//if that wasn't enough, most IDE's treat them like HTML
//so they are not greyed out, but nicely colored!
});
} ) ( jQuery, window, document );
</script>
HTML;
And this would work just fine, even the {$selector} would be replaced by PHP, the {} are optional except for method calls (unless they changed that). I put them in by habit because it colors them better in my IDE. Which is excatly how PHP treats variables in "normal" double quoted strings. (variable interpolation) except here we can use both types of quotes any way we want to...
If you do ever put one in an array it will only work this way (without the ;):
$a = [
<<<TXT
sometext
TXT
, "something else",
1,
2,
'etc..'
];
Other languages that use them (linked to the PHP section)
https://en.wikipedia.org/wiki/Here_document#PHP
In computing, a here document (here-document, here-text, heredoc, hereis, here-string or here-script) is a file literal or input stream literal: it is a section of a source code file that is treated as if it were a separate file. The term is also used for a form of multiline string literals that use similar syntax, preserving line breaks and other whitespace (including indentation) in the text.
The important thing is it does not use the quotes to define the string, so you are free to use them however you want, with no escaping.
One last thing I happened to notice from the PHP documentation that i never really read before.
the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including macOS
Maybe someone else knows, but I am not sure how important this bit really is. I program on a Windows Desktop \r\n, and then use the same exact files on a Linux server \n and Have Never 1 time had an issue with what that says. I do use editors though like Eclipse PDT, so it may default to the \n even on windows. But I have never had an issue on either one....
Enjoy!!
Probably one of the ways that works without escaping is as follows:
echo('"'."that'".'s it!"');
I've been developing with PHP for some years now, and recently came across this code:
<?php
echo <<<EOB
<html>
<head>
<title>My title</title>
</head>
...
EOB;
?>
I've never seen this approach to print HTML, which seems to be pretty useful and less prone to some weird variable or double quote syntax error.
I've searched for some official information about this and only found a post of Rasmus talking about this.
What is a detailed explanation about this functionality and what does EOB mean? Maybe end of block?
This is known as heredoc syntax. The documentation will tell you everything you need to know.
Essentially, however:
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.
The closing identifier must begin in the first column of the line. Also, the identifier 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.
So EOB is just what the author chose as his delimiter, not really sure what it stands for in his case but the identifier can be whatever you want.
Just for the sake of completeness, Heredoc in PHP is inherited from Perl, which itself inherited it from the Bourne shell.
It´s called heredoc and is described in the manual.
The official term is 'here document' I believe, usually shortened to 'heredoc'.
This is called heredoc syntax. It lets you treat large blocks of text like a string. It allows for newlines as well. Variables can be inserted into the block of text, just like using the double quotation marks for strings.
A more useful explanation can be found on PHP's own website: http://php.net/manual/en/language.types.string.php
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP <<<EOB
I saw this below piece of code in one php file , can some one explain what <<< st means.?
$status['caption']=<<<ST
ST;
P.s : I really cant google it , trust me :D
This is referred to as a heredoc string.
That is a way to store multiline strings. (Called Heredoc Syntax)
$string = <<<IDENTIFIER
IDENTIFIER;
All the lines in between are stored as string. Used for long walls of text.
It is described here.
It is called the Heredoc syntax:
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
It can be helpful for multiline strings and strings containing both double and single quotes.
As double quotes Heredoc interprets many escape sequences for special characters.
The <<< operator stands for the heredoc syntax. It's a way to write strings in a natural way.
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
That's a heredoc string ("text block").
Everything between <<<ST and ST; will be output as it is written. So you could put some HTML you want to output and save a bunch of print() statements or save your self the work of escaping characters like you would with a $variable = " textity text text text"; command.
From php website : Heredoc text behaves just like a double-quoted string, without the double quotes. This means that quotes in a heredoc do not need to be escaped, but the escape codes listed above can still be used. Variables are expanded, but the same care must be taken when expressing complex variables inside a heredoc as with strings.
I've been developing with PHP for some years now, and recently came across this code:
<?php
echo <<<EOB
<html>
<head>
<title>My title</title>
</head>
...
EOB;
?>
I've never seen this approach to print HTML, which seems to be pretty useful and less prone to some weird variable or double quote syntax error.
I've searched for some official information about this and only found a post of Rasmus talking about this.
What is a detailed explanation about this functionality and what does EOB mean? Maybe end of block?
This is known as heredoc syntax. The documentation will tell you everything you need to know.
Essentially, however:
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.
The closing identifier must begin in the first column of the line. Also, the identifier 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.
So EOB is just what the author chose as his delimiter, not really sure what it stands for in his case but the identifier can be whatever you want.
Just for the sake of completeness, Heredoc in PHP is inherited from Perl, which itself inherited it from the Bourne shell.
It´s called heredoc and is described in the manual.
The official term is 'here document' I believe, usually shortened to 'heredoc'.
This is called heredoc syntax. It lets you treat large blocks of text like a string. It allows for newlines as well. Variables can be inserted into the block of text, just like using the double quotation marks for strings.
A more useful explanation can be found on PHP's own website: http://php.net/manual/en/language.types.string.php