echo html 和变量的语法帮助
我有这段代码(见下文),我无法回显变量。第 5 行是一个 echo,在该 echo 中是一个 html 负载(我已经转义了引号)和一个变量负载。我无法让变量在主回显中回显。
更新: 其中还有一个需要考虑的 onclick。
<?php
if(
in_array("Branding", get_field('categories')) && $grid_title == "Branding"
){
echo "
<div class=\"grid-box\" onclick=\"location.href='<?php echo get_page_link($post->ID) ?>';\" style=\"cursor: pointer;\">
<div class=\"phase-1\">
<img class=\"grid-image\" src=\"<?php echo $fields->thumb_image; ?>\" alt=\"<?php echo $fields->company_name; ?>\" height=\"152\" width=\"210\" />
<div class=\"grid-heading\">
<h2><?php echo $fields->company_name; ?></h2>
<h3><?php echo implode(', ',get_field('categories'));?></h3>
</div>
</div>
<div class=\"phase-2\">
<div class=\"grid-info\">
<h4><?php echo $fields->project_name; ?></h4>
<p><?php echo $fields->description; ?></p>
</div>
<div class=\"grid-heading-hover\">
<h2><?php echo $fields->company_name; ?></h2>
<h3><?php echo implode(', ',get_field('categories'));?></h3>
</div>
</div>
</div>
";
}
?>
I have this code (see below) that I'm having trouble echoing the variables out. On the 5th line is an echo, within that echo is a load of html (which I've escaped the quotation marks) and a load of variables. I can't get the variables to echo out within the main echo.
Update:
Also within there is an onclick that needs to be taken into account.
<?php
if(
in_array("Branding", get_field('categories')) && $grid_title == "Branding"
){
echo "
<div class=\"grid-box\" onclick=\"location.href='<?php echo get_page_link($post->ID) ?>';\" style=\"cursor: pointer;\">
<div class=\"phase-1\">
<img class=\"grid-image\" src=\"<?php echo $fields->thumb_image; ?>\" alt=\"<?php echo $fields->company_name; ?>\" height=\"152\" width=\"210\" />
<div class=\"grid-heading\">
<h2><?php echo $fields->company_name; ?></h2>
<h3><?php echo implode(', ',get_field('categories'));?></h3>
</div>
</div>
<div class=\"phase-2\">
<div class=\"grid-info\">
<h4><?php echo $fields->project_name; ?></h4>
<p><?php echo $fields->description; ?></p>
</div>
<div class=\"grid-heading-hover\">
<h2><?php echo $fields->company_name; ?></h2>
<h3><?php echo implode(', ',get_field('categories'));?></h3>
</div>
</div>
</div>
";
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需在 if 之后关闭 php 标签,并在结束分号之前打开它即可。
Just close the php tag after the if and open it before the closing semi-colon.
这样的事情
是行不通的。
相反,您可以“闯入和退出” php,例如:
或使用 s/printf
或使用字符串连接:
或使用某种模板代码。
Something like this
will not work.
Instead you could "break in and out" of php, like:
Or use s/printf
Or use string concatenation:
Or use some kind of templating code.
您不能在 echo 中使用 echo,您只需要正确连接字符串,如下所示:
You can't use echo within an echo, you just need to concatenate your strings properly like so:
你这里好像有一点误会。
在 PHP 中,您可以通过
外部编写简单的 HTML 来输出 HTML,也可以
echo
/ < 等方式 在内部插入一些 PHP 代码。 code>print 输出 PHP 字符串包括 HTML但你不能混淆两者。
所以这不好:
因为这是一个字符串,并且在字符串中您无法打开
部分。您应该这样做:
这是在字符串中插入 PHP 变量的很酷的方法之一。
You seem to have a little misunderstanding here.
In PHP you can output HTML either by
<?php...?>
, and possibly inserting some PHP code insideecho
/print
to output a PHP string which includes HTMLBut you cannot mix up the two.
So this is not good:
Because this is a string, and in a string you can not open a
<?php
section. You should do something like this instead:which is one of the cool ways to insert PHP variables inside a string.