问题描述
我可以在PHP中使用什么代替普通的'
和(周围没有(或'或')符号)?
What could I use in PHP in place of the normal '
and (without ' or ") symbols around something?
示例:
echo("Hello, World!")
推荐答案
封装字符串有4种方法,单引号'
,双引号"
, heredoc 和 nowdoc .
There are 4 ways to encapsulate strings, single quotes '
, double quotes "
, heredoc and nowdoc.
http://www.php.net/manual/zh-CN/language.types.string.php#language.types.string.syntax.heredoc
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
Nowdoc
用相同的<<<<用于heredocs的序列,但其后的标识符用单引号引起来,例如<<<'EOT'.关于Heredoc标识符的所有规则也适用于nowdoc标识符,尤其是那些与结束标识符的外观有关的规则.
A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. All the rules for heredoc identifiers also apply to nowdoc identifiers, especially those regarding the appearance of the closing identifier.
http://www.php.net/manual/zh-CN/language.types.string.php#language.types.string.syntax.nowdoc
$str = <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
EOD;
转义
如果要在单引号或双引号字符串中使用文字单引号或双引号,则必须转义它们:
Escaping
If you want to use literal single or double quotes within single or double quoted strings, you have to escape them:
$str = '\''; // single quote
$str = "\""; // double quote
如赫伯特所说,您不必在双引号引起来的字符串中转义单引号,也不必在单引号引起来的字符串中转义双引号.
As Herbert noted, you don't have to escape single quotes within a double quoted strings and you don't have to escape double quotes within a single quoted string.
如果必须大量添加引号,请使用 addslashes()函数:
If you have to add quotes on a large scale, use the addslashes() function:
$str = "Is your name O'reilly?";
echo addslashes($str); // Is your name O\'reilly?
这篇关于有没有其他方法可以在PHP中编写字符串文字(不带'或')?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!