In PHP, how do I create a multi-line variable without escaping quotes?

泄露秘密 提交于 2019-12-11 12:59:57

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!