1
<?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!

1

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.