PHP 简明教程

PHP 转义字符

1. PHP 转义字符

要在字符串中插入非法字符,您需要使用转义字符(Escape Character)。

在 PHP 中,转义字符是一个反斜杠 \,后面紧跟您想要插入的字符。

非法字符的一个常见例子是:在被双引号包围的字符串内部,再次使用双引号。

错误示例

$x = "We are the so-called "Vikings" from the north.";
echo $x;

为了解决上述语法冲突问题,请使用转义字符 \"

示例

$x = "We are the so-called \"Vikings\" from the north.";
echo $x;

2. 常用转义字符列表

以下是 PHP 中其他常用的转义字符序列:

代码描述
\'单引号 (Single Quote)
\"双引号 (Double Quote)
\$PHP 变量符号 (PHP variables)
\n换行符 (New Line)
\r回车符 (Carriage Return)
\t制表符 (Tab)
\f换页符 (Form Feed)
\ooo八进制值 (Octal value)
\xhh十六进制值 (Hex value)