可能重复的html:
Difference between single quote and double quote string in php如何将PHP内通过PHP产生
我需要包括我通过PHP产生一些HTML文件中的一些PHP代码。
我试过了以下,但这不起作用。你能向我解释为什么?
<?php
$text='mc';
echo '<text>"$text"</text>'
?>
可能重复的html:
Difference between single quote and double quote string in php如何将PHP内通过PHP产生
我需要包括我通过PHP产生一些HTML文件中的一些PHP代码。
我试过了以下,但这不起作用。你能向我解释为什么?
<?php
$text='mc';
echo '<text>"$text"</text>'
?>
因为你要在这种情况下使用echo "<text>\"$text\"</text>"
了解更多关于不同类型的PHP字符串中this question和the manual
你也可以使用字符串连接,像这样: echo '<text>' . $text . '</text>'
你喜欢哪种方式完全取决于你。
它应该是:
<?php
$text='mc';
echo '<text>'.$text.'</text>';
?>
<?php
$text='mc';
echo '<text>'.$text.'</text>'
//or
echo "<text>$text</text>";
?>
你能解释为什么这回答了这个问题。 – ChrisF
你应该注意到单引号和双引号之间的使用差异。以下资源将帮助您。 http://php.net/manual/en/language.types.string.php
有几个选项:
<?php
$text = 'mc';
echo '<text>'.$text.'</text>';
echo '<text>',$text,'</text>';
echo "<text>$text</text>";
echo "<text>{$text}</text>";?>
您可以参考string manual在php.net以便今后使用。
谢谢你真的很有用 – Ozan
另请阅读[heredoc](http://php.net/heredoc)字符串。 – mario
echo'“ $ text ”;'也行。 –