在 PHP 中连接 HTML 字符串和变量 [英] Concatenate HTML string and variable in PHP

查看:28
本文介绍了在 PHP 中连接 HTML 字符串和变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这种连接HTML字符串和变量的解决方案不方便,因为我们需要中断字符串:

This solution to concatenate HTML string and variable is not convenient because we need to interrupt the string:

$content = '<meta property="foo" url="' . $url . '" name="' . $name . '">'; 

而这个不方便,因为我们必须通过 \" 转义 ":

whereas this one is not handy because we have to escape " by \":

$content = "<meta property=\"foo\" url=\"$url\" name=\"$name\">";

您会使用哪些其他解决方案(不需要额外功能)来提高代码可读性?

注意:不是heredoc相关问题的重复,因为我在这里寻找单行解决方案.

Note: not a duplicate of heredoc related questions because here I'm looking for a one-line solution.

推荐答案

对原始 HTML 使用单引号,对 PHP 变量使用花括号.像这样;

Use single quotes for raw HTML and curly braces around PHP variables. Like so;

$content = "<meta property='foo' url='{$url}' name='{$name}'>";

这篇关于在 PHP 中连接 HTML 字符串和变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆