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.
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.
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;