PHP - 可以将一长串HTML包装在PHP变量中吗?

Ada*_*eck 2 php variables

可以这样做 - 用一个PHP标签打开一个变量,然后关闭PHP标签,但保持变量打开,以便下面的所有内容成为变量的值?或者PHP变量大小/字符是否有限制?

<?php $content = " ?>

a bunch of content goes here <br />
with lots of HTML tags and JS scripts

<?php "; ?>
Run Code Online (Sandbox Code Playgroud)

Gor*_*don 8

你可以使用HEREDOC/NOWDOC

$content = <<< 'HTML'

a bunch of content goes here <br />
with lots of HTML tags and JS scripts

HTML;
Run Code Online (Sandbox Code Playgroud)

输出缓冲,例如

<?php ob_start(); ?>

foo

<?php
    $var = ob_get_clean();
    var_dump($var); // will contain foo and surrounding whitespace
Run Code Online (Sandbox Code Playgroud)


Fan*_*nis 6

不,但你可以用heredoc做一些

$content = <<< END
some content here<br/>
<script type="text/javascript">
alert('hi');
</script>

END;
Run Code Online (Sandbox Code Playgroud)