问题
In CFML there is the construct:
<cfsavecontent name="HTML_content">
<div class="test_div" style="color:#FFF;">Test</div>
</cfsavecontent>
Within PHP there seems to be no way of doing this without some really hacky function or escaping double quotes. I'm trying to create emails (large html documents) within the same page by saving the email content and simply mailing it but it seems like there's no way of doing this within PHP without using $var = "\"bad code\"";
Can somebody point me in the right direction?
I'v tried the answer by deceze below
$var = <<<HTML
<div class="test_div" style="color:#FFF;">Test</div>
HTML;
But I get the following error:
Parse error: syntax error, unexpected $end, expecting T_VARIABLE or T_END_HEREDOC or T_DOLLAR_OPEN_CURLY_BRACES or T_CURLY_OPEN in C:\vhosts\jpl\pages\about\testimonials.php on line 29
For anyone else that is stuck on this, The HTML; part at the end of the HEREDOCS example needs to be right at the beginning (no tabs, spaces or anything) of the end.
So newline HTML; not newline [tab or space] HTML;
回答1:
$var = '<div class="test_div" style="color:#FFF;">Test</div>';
$var = <<<HTML
<div class="test_div" style="color:#FFF;">Test</div>
HTML;
So many ways to quote a string... http://php.net/manual/en/language.types.string.php
Also consider putting the email contents into a separate file and includeing it where needed.
来源:https://stackoverflow.com/questions/13540533/in-php-how-do-i-create-a-multi-line-variable-without-escaping-quotes