【发布时间】:2018-09-11 04:07:26
【问题描述】:
我们通过<?php ... ?>标签在HTML代码中编写PHP代码。因此,通常情况下,如果您可以在需要的行中退出 PHP,那么在已经存在于 HTML 代码中的 PHP 代码中编写 HTML 代码是没有意义的。但是,如果您需要将 HTML 代码与 PHP 代码放在同一行中怎么办?
我的例子是这样的:
<div>
<?php ($bool) ? <script>...</script> : <script></script> ?>
</div>
这是:
<div>
<?php if($bool): ?>
<script>...</script>
<?php else: ?>
<script>...</script>
<?php endif; ?>
</div>
唯一的办法?
注意:您可以使用 <h1>、<strong>、<title> 或任何其他“单行”来代替 <script>。
提前谢谢你。
【问题讨论】:
-
将你的 html 放入 php 字符串并回显它们。或者做第二个 sn-p 之类的事情。
-
有多种方法可以做到这一点。请参阅php.net/manual/en/language.types.string.php 以获取长列表。为什么一定要
one-liner? -
是的,效果很好。
<div>\n \t<?php echo ($bool) ? "<script>...</script>" : "<script>...</script>"; ?>\n</div>完成了这项工作。
标签: php conditional-operator inline-scripting