user3589620
user3589620

Reputation:

PHP HTML Heredoc

<?php
$html = <<< HTM
<h1>This works!</h1>
HTM;

echo $html;

$html2 = <<< HTM2 
<h3>And this don't</h3>
HTM2;

echo $html2;
?>

There are two heredocs. The first heredoc does work, the second not. And I don't understand why. Maybe someone can help to figure out this problem. Thank you very much!

Upvotes: 1

Views: 405

Answers (1)

Luka
Luka

Reputation: 1718

Try this:

<?php
$html = <<< HTM
<h1>This works!</h1>
HTM;

echo $html;

$html2 = <<< HTM2
<h3>And this don't</h3>
HTM2;

echo $html2;
?>

You've got space after HTM2, here: <<< HTM2. The space after <<< isn't a problem, just the one right after HTM2 in your example. Remove it and you'll be fine. You should always make sure there is no whitespace around the heredoc delimiters. It can be tricky at times. Please refer to the documentation for more information: PHP Heredoc Documentation.

Upvotes: 2

Related Questions