1

I need the same html-text in different places of my script so I want to save my html text in a php variable. Kinda like this:

HTML:

<div>
   <p>bla</p>
</div>

-->php:

$html="<div>\n\t<p>bla</p>\n</div>";

There are already some tools like this which transforms html text into multiple lines of php echo code. Is there also a tool for a more compact solution like in my example?

user2718671
  • 2,866
  • 9
  • 49
  • 86
  • 3
    [heredoc](http://prototype.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc) or nowdoc formats for strings – Mark Baker Dec 19 '13 at 10:47

1 Answers1

7

You can use NOWDOC (available since PHP 5.3.0) formatting to store the text:

$html = <<<'HTML'
<div>
   <p>bla</p>
</div>
HTML;

If you want to use variables in your text, then you can use HEREDOC instead:

$html = <<<HTML
<div>
   <p>{$variable}</p>
</div>
HTML;

See also: Advantages / inconveniences of heredoc vs nowdoc in php

Community
  • 1
  • 1
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • Awesome! That is exactly what I was looking for. Thanks! But how could I embed a php echo command (with a function) inside my html text? I got this code: within my html text. Replacing with {} did not work. – user2718671 Dec 19 '13 at 11:04
  • oh lol. solved it with $variable=plugins_url('plugin_name/img/filename.png'); - with using $variable in the html text it was parsed – user2718671 Dec 19 '13 at 11:51