Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of <<<EOD in PHP?

Tags:

syntax

php

I am implementing node to PDF using Drupal and tcpdf. In such case I am suppose to use this <<<EOD tag. If I don't use it, it throws error. I can't exactly get the purpose of <<<EOD.

Could anybody please explain the concept of this?

$html = <<<EOD         <tr>           <td>TEST</td>         </tr> EOD; 
like image 850
Fero Avatar asked Aug 03 '11 09:08

Fero


People also ask

What is use of EOD in PHP?

EOD = End Of Data, EOT = End of Text.

What is the purpose of using the heredoc and Nowdoc in PHP?

Heredoc and nowdoc provide useful alternatives to defining strings in PHP to the more widely used quoted string syntax. They are especially useful when we need to define a string that spans multiple lines (new lines are also interpreted when used in quoted strings) and where use of whitespace is important.

What is heredoc and why might it be useful?

A heredoc is a way to define a multiline string, while maintaining the original indentation & formatting. You can use a Heredoc to embed snippets of SQL, HTML, or even XML code. A Heredoc starts with <<- , followed by a word that represents the name for the heredoc, then the contents.

Should I use heredoc?

Heredoc's are a great alternative to quoted strings because of increased readability and maintainability. You don't have to escape quotes and (good) IDEs or text editors will use the proper syntax highlighting.


2 Answers

That is not HTML, but PHP. It is called the HEREDOC string method, and is an alternative to using quotes for writing multiline strings.

The HTML in your example will be:

    <tr>       <td>TEST</td>     </tr> 

Read the PHP documentation that explains it.

like image 89
Pelle Avatar answered Oct 18 '22 03:10

Pelle


there are four types of strings available in php. They are single quotes ('), double quotes (") and Nowdoc (<<<'EOD') and heredoc(<<<EOD) strings

you can use both single quotes and double quotes inside heredoc string. Variables will be expanded just as double quotes.

nowdoc strings will not expand variables just like single quotes.

ref: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

like image 26
Poomalairaj Avatar answered Oct 18 '22 03:10

Poomalairaj