Store inline HTML within variable in PHP
I was wondering, mostly because I think I've seen it before somewhere, if it is possible to store HTML within a variable, something like the following (I know this makes no sense, it's just to clarify my question):
<? $var = '开发者_StackOverflow社区 ?>
text goes here
<? '; ?>
And then $var
would equal text goes here
You could do that using output buffering. Have a look at the examples at ob_get_contents() and ob_start().
<? ob_start(); ?>
All kinds of stuff, maybe some <?= "php"; ?> etc.
<? $var = ob_get_contents(); ?>
You may be thinking of the Heredoc syntax:
<?php
$var = <<<EOD
text goes here
EOD;
?>
Check out this facebook blog post on XHP, a language that allows XML literals within PHP code.
精彩评论