在 html 属性中包含 php 标签



如何在 HTML 属性中包含 php 标签?我仍然觉得这很棘手。

这是 HTML 属性:

src="https://customer.site.com/?language=en_US&portal=Default"

问题是,语言应该接受一个动态值。该值存储在$lingos[$wmpl_langcode]中。

所以我一直在尝试很多变化,但我仍然卡住了。

我现在有这个,但它似乎不对。

src=<?php echo "https://customer.site.com/?language=" . $lingos[$wmpl_langcode] . "&portal=Default" ?>"

我不想再浪费时间了。任何提示都会很棒。

您只是缺少一个双引号和echo末尾的;来结束指令:

src="https://customer.site.com/?language=<?php echo $lingos[$wmpl_langcode]; ?>&portal=Default"

您现在缺少该属性的引号。

但是,这完全没问题:

src="<?php echo "https://customer.site.com/?language=" . $lingos[$wmpl_langcode] . "&portal=Default" ?>"

如果你喜欢它更干净一点,而不会混淆引号:

<img src="https://customer.site.com/?language=<?php echo $lingos[$wmpl_langcode] ?>&portal=Default"/>

你还应该考虑 urlencode((:

<img src="https://customer.site.com/?language=<?php echo urlencode($lingos[$wmpl_langcode]) ?>&portal=Default"/>

要使其"更干净",您应该使用模板引擎。

当您直接使用 echo 打印动态链接时,引号不存在。 试试这个:

src="https://customer.site.com/?language=<?=$lingos[$wmpl_langcode] ?>&portal=Default"

src="<?php echo "https://customer.site.com/?language=" . $lingos[$wmpl_langcode] . "&portal=Default" ?>"

最新更新