What Is the Use of <<<Eod in PHP

What is the use of EOD in PHP?

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.

Can we use PHP iF Statements in EOD syntax code

No, because everything inside the <<< block (known as a "HEREDOC") is a string.

If you write the code in the question, you'll be writing a string containing PHP code, which isn't what you want (I hope).

Do your logic outside of the HEREDOC, and use plain variables inside it:

if(isset($variablename)) {
$outputVar = "...some text";
} else {
$outputVar = "...some text";
}

print <<<EOD
<h3>Caption</h3>
{$outputVar}
EOD;

How to use EOD as a array value

HEREDOC doesn't take quotes.

    'en' => <<<EOD
error no: %s.
EOD
];

Note that there must be no space before the EOD marker, nor must there be anything after it.

If you want multiple array elements like this, you will need to put the comma separating the elements on a separate line.

    'en' => <<<EOD
error no: %s.
EOD
, 'fr' => <<<EOD
erreur nº: %s.
EOD
]

What does EOD acronym stand for?

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

Heredoc: what does the commonly used 'EOT' actually mean?

​It stands for "End Of Text".​

Calling PHP functions within HEREDOC strings

I would not use HEREDOC at all for this, personally. It just doesn't make for a good "template building" system. All your HTML is locked down in a string which has several disadvantages

  • No option for WYSIWYG
  • No code completion for HTML from IDEs
  • Output (HTML) locked to logic files
  • You end up having to use hacks like what you're trying to do now to achieve more complex templating, such as looping

Get a basic template engine, or just use PHP with includes - it's why the language has the <?php and ?> delimiters.

template_file.php

<html>
<head>
<title><?php echo $page_title; ?></title>
</head>
<body>
<?php echo getPageContent(); ?>
</body>

index.php

<?php

$page_title = "This is a simple demo";

function getPageContent() {
return '<p>Hello World!</p>';
}

include('template_file.php');

What is the advantage of using heredoc in PHP?

The heredoc syntax is much cleaner to me and it is really useful for multi-line strings and avoiding quoting issues. Back in the day I used to use them to construct SQL queries:

$sql = <<<SQL
select *
from $tablename
where id in [$order_ids_list]
and product_name = "widgets"
SQL;

To me this has a lower probability of introducing a syntax error than using quotes:

$sql = "
select *
from $tablename
where id in [$order_ids_list]
and product_name = \"widgets\"
";

Another point is to avoid escaping double quotes in your string:

$x = "The point of the \"argument" was to illustrate the use of here documents";

The problem with the above is the syntax error (the missing escaped quote) I just introduced as opposed to here document syntax:

$x = <<<EOF
The point of the "argument" was to illustrate the use of here documents
EOF;

It is a bit of style, but I use the following as rules for single, double and here documents for defining strings:

  • Single quotes are used when the string is a constant like 'no variables here'
  • Double quotes when I can put the string on a single line and require variable interpolation or an embedded single quote "Today is ${user}'s birthday"
  • Here documents for multi-line strings that require formatting and variable interpolation.

In PHP, what does represent?

That's heredoc syntax. You start a heredoc string by putting <<< plus a token of your choice, and terminate it by putting only the token (and nothing else!) on a new line. As a convenience, there is one exception: you are allowed to add a single semicolon after the end delimiter.

Example:

echo <<<HEREDOC
This is a heredoc string.

Newlines and everything else is preserved.
HEREDOC;

EOD parses content

<textarea> don't have a value attribute. The proper syntax would be

echo <<<EOD
<textarea>$your_content_here</textarea>
EOD;

And note that echo is not a function call. While putting (...) are not an error, they are also totally unecessary.

Also, if you're dumping html into that textarea for editing, it WILL be parsed by the browser. e.g. if the html contains a form that itself has a <textarea>...</textarea>, your contents will actually terminate the textarea prematurely. Make sure you run your html through htmlspecialchars() to prevent that. i.e

$text_to_edit = '<textarea>foo</textarea> Please fill in the text box';

# Output your editor form
echo <<<EOD
<textarea>$text_to_edit</textarea>
EOD;

will generate this for HTML:

<textarea><textarea>foo</textarea>Please fill in the text box</textarea>
a b c d

You can't nest textareas, so tag B will be ignored, tag C (from your "text to edit") will terminate the A tag, and tag D will be a dangling/illegal extra closing tag. Now your editable text has leaked out of the form, and is no longer part of the text-to-edit.



Related Topics



Leave a reply



Submit