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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6670879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:57:43  来源:igfitidea点击:

Can I echo a variable with single quotes?

phpecho

提问by RoyRoger78

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 需要回显。

回答by Jaques le Fraque

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 quotesused here!

注意这里使用的双引号

回答by Dogbert

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

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

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

回答by Felix Kling

The documentationsays:

文件说:

Note:Unlike the double-quotedand heredocsyntaxes, variablesand escape sequences for special characters will notbe expanded when they occur in single quoted strings.

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

So the answer is no, not in this way.

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

The solution is simple:

解决方法很简单:

Don't echoHTML.

不要echoHTML。

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

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

<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.
  • 不必记住转义引号。
  • 更容易维护 HTML 代码。


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

?:在这种情况下,heredoc[docs]是最糟糕的选择。

回答by You

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字符串手册,尤其是字符串解析部分。

回答by Jonathan Kuhn

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).

或者只是进出 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;

回答by Safwan Bakais

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);

回答by Er. Amit Joshi

Try this

尝试这个

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

回答by Nanne

No. 'will not parse variables. use

'将无法解析的变量。用

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

or

或者

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

回答by Igoris Azanovas

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.

这样会更干净。

回答by JamesNW

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

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

variable='my dog'

echo \''I love ' $variable\'

=> 'I love my dog'

=> '我爱我的狗'