简体   繁体   English

将内联 HTML 存储在 PHP 的变量中

[英]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):我想知道,主要是因为我想我以前在某个地方见过它,是否可以将 HTML 存储在一个变量中,如下所示(我知道这没有意义,只是为了澄清我的问题):

<? $var = ' ?>
text goes here
<? '; ?>

And then $var would equal text goes here然后$var将等于text goes here

You could do that using output buffering.您可以使用 output 缓冲来做到这一点。 Have a look at the examples at ob_get_contents() and ob_start() .查看ob_get_contents()ob_start()的示例。

<? ob_start(); ?>

All kinds of stuff, maybe some <?= "php"; ?> etc.

<? $var = ob_get_contents(); ?>

You may be thinking of the Heredoc syntax :可能正在考虑Heredoc 语法

<?php
$var = <<<EOD
text goes here
EOD;
?>

Check out this facebook blog post on XHP, a language that allows XML literals within PHP code.在 XHP 上查看这篇 facebook 博客文章,该语言允许在 PHP 代码中使用 XML 文字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM