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

Asked
Active3 hr before
Viewed126 times

7 Answers

90%

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., Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers , By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy. ,there are four types of strings available in php. They are single quotes ('), double quotes (") and Nowdoc (<<<'EOD') and heredoc(<<<EOD) strings

The HTML in your example will be:

    <tr>
       <td>TEST</td>
    </tr>
88%

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. , If the closing identifier is indented further than any lines of the body, then a ParseError will be thrown: ,Example #2 Closing identifier must not be indented further than any lines of the body, heredoc syntax

      a
      b
      c

      a
      b
      c
load more v
72%

Search Answer Titles

	$output = <<<HTML
	<p>Lorem ipsum dolor sit amet consectetur<p>
	<a href="{$foobar}">click here</a>
HTML;
load more v
65%

So what about the heredoc and nowdoc syntaxes? They provide an alternative way of defining strings in PHP. They are particularly useful when we need to define a string over multiple lines.,In PHP we have several options for specifying a string value. The two most common approaches are to use either single or double quotes. We also have heredoc and nowdoc syntaxes which are useful when we want to define a multiline string.,Nowdoc’s are equivalent to a single quoted string. To denote a nowdoc we just need to use single quotes around the opening identifier:-,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. We also don’t need to worry about escaping quote marks when using heredoc and nowdoc.

When we use a double quote string we can use escaped characters like \n which gets interpreted as a new line break and variables will get substitued for their respective values. For example:-

$foo = 'bar';
echo "Hello $foo\nGoodbye!";
// Output:-
// Hello bar
// Goodbye!
load more v
75%

Example 1: php heredoc

$output = <<<HTML
	<p>Lorem ipsum dolor sit amet consectetur<p>
	<a href="{$foobar}">click here</a>
HTML;
load more v
40%

Heredoc PHP syntax is a way to write large bloc of text inside PHP, without the classic single quote, double quotes delimiters. It relies on <<< and a token that will also mark the end of the string.,Heredoc has also the Nowdoc alternative, that is the single quote version of the Heredoc. It actually has single quotes around the token definition, and will not remplace variables inside itself.,Nowdoc is rarely used : very rarely indeed. Only 3 % of all Heredoc/Nowdoc uses the Nowdoc syntax. This may be due to the recent addition of Nowdoc (PHP 5.3), or to the classic fear of a future use that would require variables inside the string.,The most common token is OET, by far. It is supposed to mean ‘End Of Text’, which is always rather strange as an opening delimiter. Of course, it is much more useful at the end of the string.

Heredoc PHP syntax is a way to write large bloc of text inside PHP, without the classic single quote, double quotes delimiters. It relies on <<< and a token that will also mark the end of the string.

< ? php
22%

In practice, you use the heredoc syntax to define a string that contains a single quote, double quotes, or variables. The heredoc string makes the string easier to read.,The following shows the syntax of a heredoc string:,When you place variables in a double-quoted string, PHP will expand the variable names. If a string contains the double quotes (“), you need to escape them using the backslash character(\). For example:,PHP heredoc strings behave like double-quoted strings, without the double-quotes. It means that they don’t need to escape quotes and expand variables. For example:

When you place variables in a double-quoted string, PHP will expand the variable names. If a string contains the double quotes (“), you need to escape them using the backslash character(\). For example:

.wp - block - code {
      border: 0;
      padding: 0;
   }

   .wp - block - code > div {
      overflow: auto;
   }

   .shcb - language {
      border: 0;
      clip: rect(1 px, 1 px, 1 px, 1 px); -
      webkit - clip - path: inset(50 % );
      clip - path: inset(50 % );
      height: 1 px;
      margin: -1 px;
      overflow: hidden;
      padding: 0;
      position: absolute;
      width: 1 px;
      word - wrap: normal;
      word - break: normal;
   }

   .hljs {
      box - sizing: border - box;
   }

   .hljs.shcb - code - table {
      display: table;
      width: 100 % ;
   }

   .hljs.shcb - code - table > .shcb - loc {
      color: inherit;
      display: table - row;
      width: 100 % ;
   }

   .hljs.shcb - code - table.shcb - loc > span {
      display: table - cell;
   }

   .wp - block - code code.hljs: not(.shcb - wrap - lines) {
      white - space: pre;
   }

   .wp - block - code code.hljs.shcb - wrap - lines {
      white - space: pre - wrap;
   }

   .hljs.shcb - line - numbers {
      border - spacing: 0;
      counter - reset: line;
   }

   .hljs.shcb - line - numbers > .shcb - loc {
      counter - increment: line;
   }

   .hljs.shcb - line - numbers.shcb - loc > span {
      padding - left: 0.75 em;
   }

   .hljs.shcb - line - numbers.shcb - loc::before {
      border - right: 1 px solid #ddd;
      content: counter(line);
      display: table - cell;
      padding: 0 0.75 em;
      text - align: right; -
      webkit - user - select: none; -
      moz - user - select: none; -
      ms - user - select: none;
      user - select: none;
      white - space: nowrap;
      width: 1 % ;
   } < ? php

$he = 'Bob';
$she = 'Alice';

$text = "$he said, \"PHP is awesome\".\
"Of course.\" $she agreed.";

echo $text;
Code language: PHP(php)
load more v

Other "undefined-undefined" queries related to "What is the use of <<<EOD in PHP?"