我可以用单引号回显变量吗?

发布于 2024-11-19 20:44:48 字数 120 浏览 4 评论 0 原文

我可以用单引号回显变量吗?

例如

echo 'I love my $variable.';

我需要这样做,因为我也有很多 HTML 需要回显。

Can I echo in single quotes with a variable?

example

echo 'I love my $variable.';

I need to do this because I have lots of HTML to echo too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

不及他 2024-11-26 20:44:48

您必须使用:

echo 'I love my ' . $variable . '.';

此方法将变量附加到字符串。

或者您可以使用:

echo "I love my $variable.";

注意这里使用的双引号

You must either use:

echo 'I love my ' . $variable . '.';

This method appends the variable to the string.

Or you could use:

echo "I love my $variable.";

Notice the double quotes used here!

皇甫轩 2024-11-26 20:44:48

如果有很多文本,请查看 Heredoc 语法。

$var = <<<EOT
<div style="">Some 'text' {$variable}</div>
EOT;

If there's a lot of text, have a look at the Heredoc syntax.

$var = <<<EOT
<div style="">Some 'text' {$variable}</div>
EOT;
梦情居士 2024-11-26 20:44:48

文档说:

注意:双引号heredoc 语法,变量 和当特殊字符出现在单引号字符串中时,特殊字符的转义序列将不会被扩展。

所以答案是否定的,不是这样的。

解决方案很简单:

不要echo HTML。

只要您不创建类似部分视图或东西,就没有理由回显 HTML。将 PHP 嵌入到 HTML 中:

<div class="foo">
    <span><?php echo $value; ?></span>
</div>

好处是:

  • 不必记住引号的转义。
  • 更容易维护 HTML 代码。

†:在本例中 heredoc [docs] 是最不糟糕的选择。

The documentation says:

Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

So the answer is no, not in this way.

The solution is simple:

Don't echo HTML.

As long you are not creating like partial views or stuff , there is no reason to echo HTML. Embed PHP into HTML instead:

<div class="foo">
    <span><?php echo $value; ?></span>
</div>

The benefits are:

  • Don't have to remember escaping of quotes.
  • Easier to maintain HTML code.

†: In this case heredoc [docs] is the least worse alternative.

吲‖鸣 2024-11-26 20:44:48

不会。' 不会解析变量。使用

echo 'I love my '.$variable.'.';

echo "I love my {$variable}. And I have \" some characters escaped";

No. ' will not parse variables. use

echo 'I love my '.$variable.'.';

or

echo "I love my {$variable}. And I have \" some characters escaped";
叶落知秋 2024-11-26 20:44:48

不可以。变量(和特殊字符)仅在用双引号分隔的字符串中扩展。如果您想在单引号分隔的字符串中包含变量,则必须将其连接起来:

echo 'I love my '.$variable.'.';

如果您希望使用由双引号分隔的字符串,也可以转义 HTML 的双引号:

echo "<a href=\"$url\">$text</a>";

请参阅 PHP 手册 关于字符串,特别是字符串解析,了解更多信息。

No. Variables (and special characters) only expand in strings delimited with double quotes. If you want to include a variable in a single-quote delimited string, you have to concatenate it instead:

echo 'I love my '.$variable.'.';

You could also escape the double quotes of your HTML if you'd rather use strings delimited by doble quotes:

echo "<a href=\"$url\">$text</a>";

See the PHP manual on strings, especially the part on string parsing, for more information.

巨坚强 2024-11-26 20:44:48

不在字符串内,但您可以退出字符串并连接变量。

echo 'I love my '.$variable.'.';

另一个选项是转义双引号。

echo "I love my \"$variable\".";

或者只是进出 php 来回显变量(虽然丑陋)。

I love my <?php echo $variable; ?>.
With short tags enabled, I love my <?=$variable?>.

最后,这里有一个允许单引号和双引号以及变量的heredoc格式。

echo <<<HTML
I love my $variable. '"...
HTML;

not within the string, but you can exit the string and concatenate the variable in.

echo 'I love my '.$variable.'.';

The other option would just be to escape the double quotes.

echo "I love my \"$variable\".";

Or just go in and out of php to echo variables (although ugly).

I love my <?php echo $variable; ?>.
With short tags enabled, I love my <?=$variable?>.

Lastly there is heredoc format that allows both single and double quotes along with variables.

echo <<<HTML
I love my $variable. '"...
HTML;
负佳期 2024-11-26 20:44:48

试试这个

<php echo 'I love my '. $variable . ' .'; ?>

Try this

<php echo 'I love my '. $variable . ' .'; ?>
听你说爱我 2024-11-26 20:44:48

摘自 php.net:
“注意:与双引号和定界符语法不同,特殊字符的变量和转义序列在单引号字符串中出现时不会被扩展。”

所以答案是否定的,你不能。但你可以这样做:

echo 'I love my' . $variable . '.';

这样会更干净。

Taken from php.net:
"Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings."

So the answer is no you can't. But you can do it like this :

echo 'I love my' . $variable . '.';

It will be cleaner this way.

挥剑断情 2024-11-26 20:44:48

除了其他用户提到的解决方案(例如字符串连接)之外,您还可以使用占位符,如下所示;

请注意 %s 代表字符串。

$variable = 'what ever'; 
printf('The $variable is %s', $variable);

Beside the solutions mentioned by other users such as string concatenation, you can use placeholders as follows;

Note the %s is for string.

$variable = 'what ever'; 
printf('The $variable is %s', $variable);
祁梦 2024-11-26 20:44:48

就我而言,我更改了命令,如下所示,它对我有用:在每个单引号(')旁边添加双引号两次(“”)

PVALUE='1,2,3'
echo "variable='""${PVALUE},repmgr'""" >>postgresql.conf
  • 输出:

    变量='1,2,3,repmgr'

#如何在单引号 bash shell 脚本中打印 echo 中的变量

In my case, I have changed my command as follows and it worked for me:Added double quotes twice("") beside every single quote(')

PVALUE='1,2,3'
echo "variable='""${PVALUE},repmgr'""" >>postgresql.conf
  • Output:

    variable='1,2,3,repmgr'

#How to print a variable in echo within a single quote bash shell script

冧九 2024-11-26 20:44:48

使用转义单引号将文本字符串括起来,如下所示:

variable='my dog'

echo \''I love ' $variable\'

=>
“我爱我的狗”

use the escape sing-quote to surround the text string as:

variable='my dog'

echo \''I love ' $variable\'

=>
'I love my dog'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文