简体   繁体   English

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

[英]Can I echo a variable with single quotes?

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.我需要这样做,因为我也有很多 HTML 要回显。

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!注意这里使用的双引号

If there's a lot of text, have a look at the Heredoc syntax.如果有很多文本,请查看 Heredoc 语法。

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

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.注意:双引号heredoc语法不同,特殊字符的变量和转义序列在出现在单引号字符串中时不会被扩展。

So the answer is no, not in this way.所以答案是否定的,不是这样。

The solution is simple:解决方案很简单:

Don't echo HTML.不要echo显 HTML。

As long you are not creating like partial views or stuff , there is no reason to echo HTML.只要您不创建部分视图或东西 ,就没有理由回显 HTML。 Embed PHP into HTML instead:将 PHP 嵌入到 HTML 中:

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

The benefits are:好处是:

  • Don't have to remember escaping of quotes.不必记住报价的 escaping。
  • Easier to maintain HTML code.更容易维护 HTML 代码。

†: In this case heredoc [docs] is the least worse alternative. †:在这种情况下, heredoc [docs]是最不差的选择。

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:如果您更愿意使用由双引号分隔的字符串,您也可以转义 HTML 的双引号:

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

See the PHP manual on strings , especially the part on string parsing , for more information.有关详细信息,请参阅 PHP字符串手册,尤其是字符串解析部分。

Beside the solutions mentioned by other users such as string concatenation, you can use placeholders as follows;除了其他用户提到的字符串连接等解决方案外,您还可以使用占位符,如下所示;

Note the %s is for string.请注意 %s 用于字符串。

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

Try this尝试这个

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

No. ' will not parse variables. No. '不会解析变量。 use利用

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

or或者

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

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).或者只是 go 进出 php 以回显变量(虽然丑陋)。

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.最后是 heredoc 格式,它允许单引号和双引号以及变量。

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

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."摘自 php.net:“注意:与双引号和 heredoc 语法不同,特殊字符的变量和转义序列在出现在单引号字符串中时不会被扩展。”

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.这样会更干净。

use the escape sing-quote to surround the text string as:使用转义单引号将文本字符串包围为:

variable='my dog'

echo \''I love ' $variable\'

=> 'I love my dog' => '我爱我的狗'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM