[英]PHP: How can I put HTML Code with PHP Code within into a PHP Variable?
I have a question about "
and '
in PHP. I have to put a complete <li>
element into a PHP variable but this doesn't work, the output is completely false... 我在PHP中有一个关于
"
和'
的问题。我必须将一个完整的<li>
元素放入一个PHP变量中,但这是行不通的,输出是完全错误的...
$list =
"
<li class=\"<?php if($info[1] < 700) {echo \"half\";} else {echo \"full\";} ?>\">
<div class=\"onet-display\">
<?php if ($image = $post->get('image.src')): ?>
<a class=\"onet-display-block\" href=\"<?= $view->url('@blog/id', ['id' => $post->id]) ?>\"><img class=\"onet-thumb\" src=\"<?= $image ?>\" alt=\"<?= $post->get('image.alt') ?>\"></a>
<?php endif ?>
<h1 class=\"onet-thumb-title\"><a href=\"<?= $view->url('@blog/id', ['id' => $post->id]) ?>\"><?= $post->title ?></a></h1>
<div class=\"uk-margin\"><?= $post->excerpt ?: $post->content ?></div>
</div>
</li>
";
Is it because there is PHP Content in the HTML Code? 是否因为HTML代码中包含PHP内容? How can I solve this?
我该如何解决?
Can someone help me and explain why this doesn't work? 有人可以帮我解释一下为什么这行不通吗?
<?php ... <?php
Since your string contains PHP tags, I suppose you expect them to be evaluated. 由于您的字符串包含PHP标记,因此我想您希望对它们进行评估。 The opening PHP tag within another PHP tag is interpreted as a part of the PHP code.
另一个PHP标记中的开放PHP标记被解释为PHP代码的一部分。 For example, the following outputs
<?php echo time();
例如,以下输出
<?php echo time();
: :
<?php echo "<?php echo time();";
There are several ways to build a PHP string from PHP expressions. 有几种方法可以从PHP表达式构建PHP字符串。
You can create functions returning strings and concatenate the calls to them with other strings: 您可以创建返回字符串的函数,并将对它们的调用与其他字符串连接起来:
function some_function() {
return time();
}
$html = "<li " . some_function() . ">";
or use sprintf
: 或使用
sprintf
:
$html = sprintf('<li %s>', some_function());
Another way is to use eval , but I wouldn't recommend it as it allows execution of arbitrary PHP code and may cause unexpected behavior. 另一种方法是使用eval ,但我不推荐使用它,因为它允许执行任意PHP代码,并可能导致意外行为。
If you are running PHP as a template engine, you can use the output control functions , eg: 如果您将PHP作为模板引擎运行,则可以使用输出控制功能 ,例如:
<?php ob_start(); ?>
<li data-time="<?= time() ?>"> ...</li>
<?php
$html = ob_get_contents();
ob_end_clean();
echo $html;
Output 输出量
<li data-time="1483433793"> ...</li>
If, however, the string is supposed to be assigned as is, use the here document syntax : 但是,如果应该按原样分配字符串,请使用here文档语法 :
$html = <<<'HTML'
<li data-time="{$variable_will_NOT_be_parsed}">...</li>
HTML;
or 要么
$html = <<<HTML
<li data-time="{$variable_WILL_be_parsed}">...</li>
HTML;
You want to store some html into a variable. 您想将一些html存储到变量中。
Your source should (if not yet) start with 您的来源应(如果尚未)以
<?php
Then you start building the contents of $list
. 然后,您开始构建
$list
的内容。
Starting from your code the nearest fix is to build $list
by appending strings: 从您的代码开始,最近的解决方法是通过附加字符串来构建
$list
:
<?php
$list = "<li class=";
if($info[1] < 700)
{
$list .= "\"half\""; // RESULT: <li class="half"
}
else
{
$list .= "\"full\""; // RESULT: <li class="full"
}
// ...and so on...
Now a couple things to note: 现在要注意几件事:
$list .= "...
is a short form of $list = $list . "...
$list .= "...
是$list = $list . "...
的缩写$list = $list . "...
Where the .
哪里了
.
dot operator joins two strings. 点运算符连接两个字符串。
Second thing you may make code easier to read by mixing single and double quotes : 第二件事, 您可以通过混合使用单引号和双引号来使代码更易于阅读 :
Use double quotes in PHP and single quotes in the generated HTML: 在PHP中使用双引号,在生成的HTML中使用单引号:
<?php
$list = "<li class=";
if($info[1] < 700)
{
$list .= "'half'"; // RESULT: <li class='half'
}
else
{
$list .= "'full'"; // RESULT: <li class='full'
}
// ...and so on...
This way you don't need to escape every double quote 这样,您就不必转义每个双引号
i think you have to work like this 我认为你必须像这样工作
$test = "PHP Text";
$list = "<strong>here ist Html</strong>" . $test . "<br />";
echo $list;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.