What is the difference between EOT and HTML? in PHP - php

I've seen some people using this code.
echo <<< EOT
Hi <br>
EOT;
and
echo <<< HTML
Hello<br>
HTML;
What's the difference of those two? and why would not they use the normal echo? like
echo "How are you<br>"?

Nothing, it's just a delimiter for the HEREDOC syntax. The only benefit of using HEREDOC is you can keep indents and structure of your string in the source code. It tends to be nicer to work with than concatenated strings - for your example Hi <br>, there is no good reason to use HEREDOC.

why would not they use the normal echo?
Using heredoc for this very example makes no sense.
And it's indeed to use echo to print out single text line.
So, nobody is using heredoc for this.
Also, in echoing large text blocks heredoc is useless again, as one have to just close PHP tag and write the text as is.
The only use of heredoc is when you need to store a large block of text in a variable.
$var = <<< HERE
Hello %s!
Please follow this link %s to continue registration.
HERE;

May be very very very late to comment but, in context of today's IDEs: When you use HEREDOCS the identifiers provide a hint as to what kind of information is being stored
Say <<<SQL the IDE's syntax highlighter may use the SQL identifier for highlighting the contents of the string as an SQL Query. <<<HTML for HTML highlighting ... and so forth.

Related

Concatenating Content between Heredoc Tags

I am converting some formatted html text to a PDF file, and in doing so, I must place a table in the document. In order to put the table into the PDF correctly, I must use heredoc tags instead of regulars quotations (Came to this conclusion through some experimentation). What I am wondering is whether I can take some content from a POST call and place it between the heredoc tags a la:
$table = $_POST['table'];
$html = <<<EOD . '$table' . EOD;
Or, something of the like. I haven't come across a solution, and was wondering if this was even possible. I have tried passing the table variable directly to the method, but it is a fruitless effort. It does work if passed with the heredoc tags, however.
You want to use an Encapsulated Heredoc for this purpose. Example #5 on that page. It allows you to place the heredoc in quotes and use curly braces to utilize your variables.
PHP Example
$hey = 'Test 1 2 3';
$html = <<<"TEST"
Test me: {$hey}
TEST;
echo $html;
Returns
Test me: Test 1 2 3
Yes, like the PHP documentation says:
$name = 'Davis';
echo <<<EOT
My name is "$name". I am printing some $_POST['table'].
Now, I am printing some {$foo->bar[1]} too!
This should print a capital 'A': \x41
EOT;
You have to make sure that your text is between your starting and ending line.

Disadvantages of using EOF?

I've seen HTML nested within a PHP page written in so many ways... some of which are demonstrated in this page:
http://austingulati.com/2010/01/many-ways-to-integrate-html-into-php/
But... I very rarely see HTML and PHP written in unison as follows:
echo <<<EOF
<h1>Welcome</h1>
<p>Hello</p>
<p>{$somePHPVariable}</p>
EOF;
Question:
Is there a fundamental issue with using the EOF approach that I should be aware of?
Heredocs are wonderful if you're building HTML and need to embed variables.
They honor the linebreaks/spacing you embed within them (even if the browser doesn't display that) so it's MUCH easier to build nicely formatted HTML, and also relieve you of the need to escape quotes while building the strings:
e.g. compare
print("<div class=\"this\">\n\tblah blah\n\t\t<span class=\"that\">blah</span>\n</div>");
v.s.
echo <<<EOL
<div class="this">
blah blah
<span class="that"</span>
</div>
EOL;
They can also be used in concatenation operations, eg.
$x = "hello";
$x .= <<<EOL
there, how
EOL
$x .= <<<EOL
are you?
EOL;
will eventually give $x the value hello there, how are you?. Basically consider the heredoc syntax to be a VERY fancy version of double-quoted strings, with none of the drawbacks. The only restriction is that the heredoc's sentinal value must be on a line by itself, so there's no way to make a "one line" heredoc.
This is called heredoc syntax (the "EOF" can be any identifier, not just "EOF"). It's a little more finicky than the other string syntaxes, and it can be a little confusing to folks who haven't encountered it before. Perfectly fine to use, though.
Heredoc syntax is actually great! It is very useful for situations where you have data with lots of single quotes and double quotes.
I've gone into some of the details of it before here:
Quoting quotation marks
One of the downsides to it I find is that it does tend to disable syntax highlighting in editors because the editor views it as one big string and therefore won't properly highlight html inside.
Another downside is not being able to directly call functions from within heredoc syntax. There are workarounds and a few of them are mentioned here: Calling PHP functions within HEREDOC strings

PHP - Comments inside <?php echo <<<EOF

How do I comment out if I use this syntax?:
<?php
echo <<<EOF
Is it //Comment or <!-- Comment --> or something else?
EOF;
?>
I'm a little bit confused.
Thank you
You can't. The point of HEREDOC is that anything inside it will be part of the string. It exists to avoid having PHP meta characters (including comments) treated as such. Anything you put inside it will appear in the string (and thus, in this instance, be echoed to wherever the output is directed).
If the output is HTML, then you could include an HTML comment in it. That will still appear in the output, but anything parsing the HTML will treat it as a comment. Likewise, if the content is JS then you can use a JS comment, and so on.
You can't use a comment inside the heredoc syntax.
http://en.wikipedia.org/wiki/Here_document
It's a way to specify a literal string, literally.
Everything between the heredoc delimiters is interpreted literally, and that's the point of the heredoc syntax. Any HTML comments will be outputted as well, and PHP doesn't care that the browser will omit them.
It depends on the type of output you are echoing to. If you're echoing that data to an HTML page, you can use the <!-- --> syntax and the browser will see that as a comment. If you're outputting to a plaintext file, everything within the heredoc will be output (in truth, everything will be output when writing HTML as well, just the browser will interpret the HTML comment).
When I'm using heredoc syntax and need to comment the information inside, I typically use a (PHP-style) comment before the heredoc and reference any specific lines within by their line number, relative to the heredoc:
/* Write out default INI file.
* L002: Log level. Possible values: debug,info,warning,error.
*/
echo <<<EOF
[global]
logging = error
...
EOF
Hope that helps.
To my knowledge you can't put comments inside the HEREDOC block.
According to PHP documentation at http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc "Heredoc text behaves just like a double-quoted string, without the double quotes.".. and you can't put comments inside a double-quoted string.
Actually, there is a circuitous way of commenting inside an echo.
You can use the following syntax
<?php
'.(/*comment here*/ NULL).'
This line will be read as inner php and can contain comments. NULL has to be included because everything inside the inner php requires a value. NULL is the most convenient choice, since you dont actually want a value, just a comment.
Actually, you cannot use PHP comments in the middle of the string (neither in double quoted nor in single quoted nor heredoc strings). They will be shown literally.
In some rare cases it may be useful to simulate comments and strip them later from the string. For instance it could be useful if you are creating some kind of template engine. But certainly in most cases you should not do this, as it's bad practice.
You can build up your string using "\r\n" (using double quotes for the line breaks) and put the comments on the same line after the string, e.g.
echo "line1\r\nline2"; //this outputs two lines

What is <<<_END?

I'm new to PHP and don't understand what the point of <<<_END is. Could someone please explain when this should be used? I've looked at various examples and they all seem to have HTML embedded within them. But I can use HTML without the <<<_END tags, so why should I use them? I tried searching the manual, but I keep finding the end() method for arrays.
It's the start of a heredoc. you can do:
$data = <<< _END
You can write anything you want in between the start and end
_END;
_END can be just about anything. You could put EOF or STUFF. as long as you use the same thing at the start and the finish.
This signifies the beginning of a heredoc (a multi-line string that allows you to use quotation marks in the middle, unescaped) that ends when you encounter the _END
It can be useful to define HTML in one of these if the goal is to assign it to a variable or pass it to a function rather than printing it to the web server immediately.
That syntax is called heredoc
<<<_END
some text
_END
Basically, it's a way of writing a string without worrying about escaping quotes and so on.
As you've mentioned, it doesn't really provide a lot of benefit over other string formats - although, it does mean you can write a block of HTML without escaping out of PHP with ?>
It also isn't too popular as its use generally goes against the practice of seperating content from logic by embedding the content in the middle of your script.
Does this help? http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
It allows you to echo out a block of text (just the same as with echo "words";), but without using the beginning/ending quotes, and without having to escape contained double quotes. Read the manual link above for more detail.
It's a heredoc. It's just a way of defining a string.

What is the right way to create tabs and line breaks in PHP when using single quotes?

Seems like a simple question, but I haven't been able to find a solid answer anywhere. I'm outputting a ton of HTML and find escaping "s to be error prone and hard to read, but I also want to have my HTML formatted nicely.
Want something like this (though I know this won't worK):
echo '<div id="test">\n';
echo '\t<div id="test-sub">\n';
echo '\t</div>\n';
echo '</div>\n';
What is one to do?
Thanks.
did you look on HEREDOC
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
example of advantage here : http://www.shat.net/php/notes/heredoc.php
There are a lot of ways to make sure, this works just fine for example (PHP_EOL is a cross Platt form Constant for a new line Char (EndOfLine) ):
echo "<div id=\"test\">".PHP_EOL;
echo "\t<div id=\"test-sub\">".PHP_EOL;
echo "\t</div>".PHP_EOL;
echo "</div>".PHP_EOL;
I make use of a small set of classes I wrote in order to output nicely formatted HTML. If you are interested you can find it here.
To get what you want, I would end up writing something like
$mypage = page::blank();
$mypage->opennode('div', 'id="test"');
$mypage->opennode('div', 'id="test-sub"');
$mypage->closenode(2); // div, div
echo $mypage->build_output_strict();
Another alternative would be to use a full-fledged template engine, of which there are quite a few.
use double quotes
or a multi-line echo string:
echo '<div id="test">
<div id="test-sub">
</div>
</div>';
or templates.

Categories