mirror of
https://github.com/php/doc-zh.git
synced 2026-03-24 07:02:15 +01:00
1288 lines
36 KiB
XML
1288 lines
36 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
||
<!-- $Revision$ -->
|
||
<!-- EN-Revision: 2832df2e1bd7daa1ec29ffb167dce1c9feb8cc6b Maintainer: dallas Status: ready -->
|
||
<!-- CREDITS: mowangjuanzi, Luffy -->
|
||
<sect1 xml:id="language.types.string">
|
||
<title>String 字符串</title>
|
||
|
||
<para>
|
||
一个字符串 <type>string</type> 就是由一系列的字符组成,其中每个字符等同于一个字节。这意味着
|
||
PHP 只能支持 256 的字符集,因此不支持 Unicode 。详见<link linkend="language.types.string.details">字符串类型详解</link>。
|
||
</para>
|
||
|
||
<note>
|
||
<simpara>
|
||
在 32 位版本中,<type>string</type> 最大可以达到 2GB(最多 2147483647 字节)。
|
||
</simpara>
|
||
</note>
|
||
|
||
<sect2 xml:id="language.types.string.syntax">
|
||
<title>语法</title>
|
||
|
||
<para>
|
||
一个字符串可以用 4 种方式表达:
|
||
</para>
|
||
|
||
<itemizedlist>
|
||
<listitem>
|
||
<simpara>
|
||
<link linkend="language.types.string.syntax.single">单引号</link>
|
||
</simpara>
|
||
</listitem>
|
||
<listitem>
|
||
<simpara>
|
||
<link linkend="language.types.string.syntax.double">双引号</link>
|
||
</simpara>
|
||
</listitem>
|
||
<listitem>
|
||
<simpara>
|
||
<link linkend="language.types.string.syntax.heredoc">heredoc 语法结构</link>
|
||
</simpara>
|
||
</listitem>
|
||
<listitem>
|
||
<simpara>
|
||
<link linkend="language.types.string.syntax.nowdoc">nowdoc 语法结构</link>
|
||
</simpara>
|
||
</listitem>
|
||
</itemizedlist>
|
||
|
||
<sect3 xml:id="language.types.string.syntax.single">
|
||
<title>单引号</title>
|
||
|
||
<para>
|
||
定义一个字符串的最简单的方法是用单引号把它包围起来(字符 <literal>'</literal>)。
|
||
</para>
|
||
|
||
<para>
|
||
要表达一个单引号自身,需在它的前面加个反斜线(<literal>\</literal>)来转义。要表达一个反斜线自身,则用两个反斜线(<literal>\\</literal>)。其它任何方式的反斜线都会被当成反斜线本身:也就是说如果想使用其它转义序列例如
|
||
<literal>\r</literal> 或者 <literal>\n</literal>,并不代表任何特殊含义,就单纯是这两个字符本身。
|
||
</para>
|
||
|
||
<note>
|
||
<simpara>
|
||
不像<link
|
||
linkend="language.types.string.syntax.double">双引号</link>和
|
||
<link linkend="language.types.string.syntax.heredoc">heredoc</link>
|
||
语法结构,在单引号字符串中的<link
|
||
linkend="language.variables">变量</link>和特殊字符的转义序列将<emphasis>不会</emphasis>被替换。
|
||
</simpara>
|
||
</note>
|
||
|
||
<example>
|
||
<title>Syntax Variants</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
echo 'this is a simple string', PHP_EOL;
|
||
|
||
// 可以录入多行
|
||
echo 'You can also have embedded newlines in
|
||
strings this way as it is
|
||
okay to do', PHP_EOL;
|
||
|
||
// 输出:Arnold once said: "I'll be back"
|
||
echo 'Arnold once said: "I\'ll be back"', PHP_EOL;
|
||
|
||
// 输出:You deleted C:\*.*?
|
||
echo 'You deleted C:\\*.*?', PHP_EOL;
|
||
|
||
// 输出:You deleted C:\*.*?
|
||
echo 'You deleted C:\*.*?', PHP_EOL;
|
||
|
||
// 输出:This will not expand: \n a newline
|
||
echo 'This will not expand: \n a newline', PHP_EOL;
|
||
|
||
// 输出:Variables do not $expand $either
|
||
echo 'Variables do not $expand $either', PHP_EOL;
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
|
||
</sect3>
|
||
|
||
<sect3 xml:id="language.types.string.syntax.double">
|
||
<title>双引号</title>
|
||
|
||
<para>
|
||
如果字符串是包围在双引号(<literal>"</literal>))中, PHP 将对以下特殊的字符进行解析:
|
||
</para>
|
||
|
||
<table>
|
||
<title>转义字符</title>
|
||
|
||
<tgroup cols="2">
|
||
<thead>
|
||
<row>
|
||
<entry>序列</entry>
|
||
<entry>含义</entry>
|
||
</row>
|
||
</thead>
|
||
|
||
<tbody>
|
||
<row>
|
||
<entry><literal>\n</literal></entry>
|
||
<entry>换行(ASCII 字符集中的 LF 或 0x0A (10))</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>\r</literal></entry>
|
||
<entry>回车(ASCII 字符集中的 CR 或 0x0D (13))</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>\t</literal></entry>
|
||
<entry>水平制表符(ASCII 字符集中的 HT 或 0x09 (9))</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>\v</literal></entry>
|
||
<entry>垂直制表符(ASCII 字符集中的 VT 或 0x0B (11))</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>\e</literal></entry>
|
||
<entry>Escape(ASCII 字符集中的 ESC 或 0x1B (27))</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>\f</literal></entry>
|
||
<entry>换页(ASCII 字符集中的 FF 或 0x0C (12))</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>\\</literal></entry>
|
||
<entry>反斜线</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>\$</literal></entry>
|
||
<entry>美元标记</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>\"</literal></entry>
|
||
<entry>双引号</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>\[0-7]{1,3}</literal></entry>
|
||
<entry>
|
||
八进制:匹配正则表达式序列 <literal>[0-7]{1,3}</literal> 的是八进制表示法的字符序列(比如
|
||
<literal>"\101" === "A"</literal>),会静默溢出以适应一个字节(例如 <literal>"\400" === "\000"</literal>)
|
||
</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>\x[0-9A-Fa-f]{1,2}</literal></entry>
|
||
<entry>
|
||
十六进制:匹配正则表达式序列 <literal>[0-9A-Fa-f]{1,2}</literal> 的是十六进制表示法的一个字符(比如
|
||
<literal>"\x41" === "A"</literal>)
|
||
</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>\u{[0-9A-Fa-f]+}</literal></entry>
|
||
<entry>
|
||
Unicode:匹配正则表达式 <literal>[0-9A-Fa-f]+</literal> 的字符序列是 unicode 码位,该码位能作为
|
||
UTF-8 的表达方式输出字符串。序列中必须包含大括号。例如 <literal>"\u{41}" === "A"</literal>
|
||
</entry>
|
||
</row>
|
||
</tbody>
|
||
</tgroup>
|
||
</table>
|
||
|
||
<para>
|
||
和单引号字符串一样,转义任何其它字符都会导致反斜线被显示出来。
|
||
</para>
|
||
|
||
<para>
|
||
用双引号定义的字符串最重要的特征是变量会被解析,详见<link
|
||
linkend="language.types.string.parsing">变量插值</link>。
|
||
</para>
|
||
</sect3>
|
||
|
||
<sect3 xml:id="language.types.string.syntax.heredoc">
|
||
<title>Heredoc 结构</title>
|
||
|
||
<simpara>
|
||
第三种表达字符串的方法是用 heredoc
|
||
句法结构:<literal><<<</literal>。在该运算符之后要提供一个标识符,然后换行。接下来是字符串
|
||
<type>string</type> 本身,最后要用前面定义的标识符作为结束标志。
|
||
</simpara>
|
||
|
||
<simpara>
|
||
结束标识符可以使用空格或制表符(tab)缩进,此时文档字符串会删除所有缩进。
|
||
在 PHP 7.3.0 之前的版本中,结束时所引用的标识符<emphasis>必须</emphasis>在该行的第一列。
|
||
</simpara>
|
||
<simpara>
|
||
而且,标识符的命名也要像其它标签一样遵守
|
||
PHP 的规则:只能包含字母、数字和下划线,并且必须以字母和下划线作为开头。
|
||
</simpara>
|
||
|
||
<example>
|
||
<title>PHP 7.3.0 之后的基础 Heredoc 示例</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
// 无缩进
|
||
echo <<<END
|
||
a
|
||
b
|
||
c
|
||
\n
|
||
END;
|
||
// 4 空格缩进
|
||
echo <<<END
|
||
a
|
||
b
|
||
c
|
||
END;
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs.73;
|
||
<screen>
|
||
<![CDATA[
|
||
a
|
||
b
|
||
c
|
||
a
|
||
b
|
||
c
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
|
||
<simpara>
|
||
如果结束标识符的缩进超过内容的任何一行的缩进,则将抛出 <classname>ParseError</classname> 异常:
|
||
</simpara>
|
||
|
||
<example>
|
||
<title>结束标识符的缩进不能超过正文的任何一行</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
echo <<<END
|
||
a
|
||
b
|
||
c
|
||
END;
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs.73;
|
||
<screen>
|
||
<![CDATA[
|
||
Parse error: Invalid body indentation level (expecting an indentation level of at least 3) in example.php on line 4
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
|
||
<simpara>
|
||
制表符也可以缩进结束标识符,但是,关于缩进结束标识符和内容,
|
||
制表符和空格<emphasis>不能</emphasis>混合使用。在以上任何情况下,
|
||
将会抛出 <classname>ParseError</classname> 异常。
|
||
|
||
之所以包含这些空白限制,是因为混合制表符和空格来缩进不利于易读性。
|
||
</simpara>
|
||
|
||
<example>
|
||
<title>内容(空白)和结束标识符的不同缩进</title>
|
||
<programlisting role="php" annotations="non-interactive">
|
||
<![CDATA[
|
||
<?php
|
||
// 以下所有代码都不起作用。
|
||
// 正文(空格)和结束标记(制表符),不同的缩进
|
||
{
|
||
echo <<<END
|
||
a
|
||
END;
|
||
}
|
||
// 在正文中混合空格和制表符
|
||
{
|
||
echo <<<END
|
||
a
|
||
END;
|
||
}
|
||
// 在结束标记中混合空格和制表符
|
||
{
|
||
echo <<<END
|
||
a
|
||
END;
|
||
}
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs.73;
|
||
<screen>
|
||
<![CDATA[
|
||
Parse error: Invalid indentation - tabs and spaces cannot be mixed in example.php line 8
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
|
||
<simpara>
|
||
内容字符串的结束标识符后面不需要跟分号或者换行符。
|
||
例如,从 PHP 7.3.0 开始允许以下代码:
|
||
</simpara>
|
||
|
||
<example>
|
||
<title>在结束标识符后继续表达式</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
$values = [<<<END
|
||
a
|
||
b
|
||
c
|
||
END, 'd e f'];
|
||
var_dump($values);
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs.73;
|
||
<screen>
|
||
<![CDATA[
|
||
array(2) {
|
||
[0] =>
|
||
string(11) "a
|
||
b
|
||
c"
|
||
[1] =>
|
||
string(5) "d e f"
|
||
}
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
|
||
<warning>
|
||
<simpara>
|
||
如果在某一行的开头找到了结束标识符,那么不管它是否是另外一个单词的一部分,
|
||
它都可能看作结束标识符并引起 <classname>ParseError</classname>。
|
||
</simpara>
|
||
|
||
<example>
|
||
<title>字符串内容中的结束标识符往往会导致 ParseError</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
$values = [<<<END
|
||
a
|
||
b
|
||
END ING
|
||
END, 'd e f'];
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs.73;
|
||
<screen>
|
||
<![CDATA[
|
||
Parse error: syntax error, unexpected identifier "ING", expecting "]" in example.php on line 5
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
|
||
<simpara>
|
||
为了避免这个问题,遵循以下简单的规则较为安全:
|
||
<emphasis>不要选择一个出现在文本主体中的单词作为结束标识符</emphasis>。
|
||
</simpara>
|
||
|
||
</warning>
|
||
|
||
<warning>
|
||
<simpara>
|
||
在 PHP 7.3.0 之前,请务必注意,带有结束标识符的行不能包含除
|
||
(<literal>;</literal>)外的任何其他字符。
|
||
这意味着标识符<emphasis>不能缩进</emphasis>,分号的前后也不能有任何空白或制表符。更重要的是结束标识符的前面必须是个被本地操作系统认可的换行,比如在
|
||
UNIX 和 macOS 系统中是 <literal>\n</literal>,而结束定界符之后也必须紧跟一个换行。
|
||
</simpara>
|
||
|
||
<simpara>
|
||
如果不遵守该规则导致结束标识不“干净”,PHP
|
||
将认为它不是结束标识符而继续寻找。如果在文件结束前也没有找到一个正确的结束标识符,PHP
|
||
将会在最后一行产生一个解析错误。
|
||
</simpara>
|
||
|
||
<example>
|
||
<title>PHP 7.3.0 之前的错误示例</title>
|
||
<programlisting role="php">
|
||
<!-- This is an INVALID example -->
|
||
<![CDATA[
|
||
<?php
|
||
class foo {
|
||
public $bar = <<<EOT
|
||
bar
|
||
EOT;
|
||
}
|
||
// 不能缩进标识符
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
<example>
|
||
<title>在 PHP 7.3.0 之前有效示例</title>
|
||
<programlisting role="php">
|
||
<!-- This is a VALID example -->
|
||
<![CDATA[
|
||
<?php
|
||
class foo {
|
||
public $bar = <<<EOT
|
||
bar
|
||
EOT;
|
||
}
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
|
||
<para>
|
||
Heredocs 结构不能用来初始化类的属性。
|
||
</para>
|
||
</warning>
|
||
|
||
<para>
|
||
Heredoc 结构就象是没有双引号的 <type>string</type>,这就是说在 heredoc
|
||
结构中单引号不用被转义,但是上文中列出的转义序列还可以使用。变量将被替换,但在 heredoc
|
||
结构中含有复杂的变量时要像 <type>string</type> 一样格外小心。
|
||
</para>
|
||
|
||
<example>
|
||
<title>Heredoc 结构的字符串示例</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
$str = <<<EOD
|
||
Example of string
|
||
spanning multiple lines
|
||
using heredoc syntax.
|
||
EOD;
|
||
|
||
/* 含有变量的更复杂示例 */
|
||
class foo
|
||
{
|
||
var $foo;
|
||
var $bar;
|
||
|
||
function __construct()
|
||
{
|
||
$this->foo = 'Foo';
|
||
$this->bar = array('Bar1', 'Bar2', 'Bar3');
|
||
}
|
||
}
|
||
|
||
$foo = new foo();
|
||
$name = 'MyName';
|
||
|
||
echo <<<EOT
|
||
My name is "$name". I am printing some $foo->foo.
|
||
Now, I am printing some {$foo->bar[1]}.
|
||
This should print a capital 'A': \x41
|
||
EOT;
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
My name is "MyName". I am printing some Foo.
|
||
Now, I am printing some Bar2.
|
||
This should print a capital 'A': A]]>
|
||
</screen>
|
||
|
||
<para>
|
||
也可以把 Heredoc 结构用在函数参数中来传递数据:
|
||
</para>
|
||
|
||
<example>
|
||
<title>Heredoc 结构在参数中的示例</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
var_dump(array(<<<EOD
|
||
foobar!
|
||
EOD
|
||
));
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
|
||
<para>
|
||
可以用 Heredoc 结构来初始化静态变量和类的属性和常量:
|
||
</para>
|
||
|
||
<example>
|
||
<title>使用 Heredoc 结构来初始化静态值</title>
|
||
<programlisting role="php" annotations="non-interactive">
|
||
<![CDATA[
|
||
<?php
|
||
// 静态变量
|
||
function foo()
|
||
{
|
||
static $bar = <<<LABEL
|
||
Nothing in here...
|
||
LABEL;
|
||
}
|
||
|
||
// 类的常量、属性
|
||
class foo
|
||
{
|
||
const BAR = <<<FOOBAR
|
||
Constant example
|
||
FOOBAR;
|
||
|
||
public $baz = <<<FOOBAR
|
||
Property example
|
||
FOOBAR;
|
||
}
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
|
||
<para>
|
||
还可以在 Heredoc 结构中用双引号来声明标识符:
|
||
</para>
|
||
|
||
<example>
|
||
<title>在 heredoc 结构中使用双引号</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
echo <<<"FOOBAR"
|
||
Hello World!
|
||
FOOBAR;
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
|
||
</sect3>
|
||
|
||
<sect3 xml:id="language.types.string.syntax.nowdoc">
|
||
<title>Nowdoc 结构</title>
|
||
|
||
<para>
|
||
就象 heredoc 结构类似于双引号字符串,Nowdoc 结构是类似于单引号字符串的。Nowdoc
|
||
结构很象 heredoc 结构,但是 nowdoc
|
||
中<emphasis>不进行字符串插值</emphasis>。这种结构很适合用于嵌入 PHP
|
||
代码或其它大段文本而无需对其中的特殊字符进行转义。与 SGML 的
|
||
<literal><![CDATA[ ]]></literal> 结构是用来声明大段的不用解析的文本类似,nowdoc 结构也有相同的特征。
|
||
</para>
|
||
|
||
<para>
|
||
一个 nowdoc 结构也用和 heredocs 结构一样的标记
|
||
<literal><<<</literal>, 但是跟在后面的标识符要用单引号括起来,即
|
||
<literal><<<'EOT'</literal>。Heredoc 结构的所有规则也同样适用于 nowdoc
|
||
结构,尤其是结束标识符的规则。
|
||
</para>
|
||
|
||
<example>
|
||
<title>Nowdoc 结构字符串示例</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
echo <<<'EOD'
|
||
Example of string spanning multiple lines
|
||
using nowdoc syntax. Backslashes are always treated literally,
|
||
e.g. \\ and \'.
|
||
EOD;
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
Example of string spanning multiple lines
|
||
using nowdoc syntax. Backslashes are always treated literally,
|
||
e.g. \\ and \'.
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
|
||
<example>
|
||
<title>含变量引用的 Nowdoc 字符串示例</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
|
||
/* 含有变量的更复杂的示例 */
|
||
class foo
|
||
{
|
||
public $foo;
|
||
public $bar;
|
||
|
||
function __construct()
|
||
{
|
||
$this->foo = 'Foo';
|
||
$this->bar = array('Bar1', 'Bar2', 'Bar3');
|
||
}
|
||
}
|
||
|
||
$foo = new foo();
|
||
$name = 'MyName';
|
||
|
||
echo <<<'EOT'
|
||
My name is "$name". I am printing some $foo->foo.
|
||
Now, I am printing some {$foo->bar[1]}.
|
||
This should not print a capital 'A': \x41
|
||
EOT;
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
My name is "$name". I am printing some $foo->foo.
|
||
Now, I am printing some {$foo->bar[1]}.
|
||
This should not print a capital 'A': \x41]]>
|
||
</screen>
|
||
</example>
|
||
|
||
<example>
|
||
<title>静态数据的示例</title>
|
||
<programlisting role="php" annotations="non-interactive">
|
||
<![CDATA[
|
||
<?php
|
||
class foo {
|
||
public $bar = <<<'EOT'
|
||
bar
|
||
EOT;
|
||
}
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
|
||
<note>
|
||
<para>
|
||
Nowdoc 结构是在 PHP 5.3.0 中加入的。
|
||
</para>
|
||
</note>
|
||
|
||
</sect3>
|
||
|
||
<sect3 xml:id="language.types.string.parsing">
|
||
<title>字符串插值</title>
|
||
|
||
<simpara>
|
||
当<type>字符串</type>用双引号或 heredoc 结构定义时,其中的<link
|
||
linkend="language.variables">变量</link>可以进行替换。
|
||
</simpara>
|
||
|
||
<simpara>
|
||
这里共有两种语法规则:一种<link
|
||
linkend="language.types.string.parsing.basic">基本</link>规则,一种<link
|
||
linkend="language.types.string.parsing.advanced">高级</link>规则。基本的语法规则是最常用和最方便的,它可以用最少的代码在一个
|
||
<type>string</type> 中嵌入一个变量,一个 <type>array</type> 的值,或一个 <type>object</type> 的属性。
|
||
</simpara>
|
||
|
||
<sect4 xml:id="language.types.string.parsing.basic">
|
||
<title>基本语法</title>
|
||
<simpara>
|
||
如果遇到一个美元符号(<literal>$</literal>),后面的字符会被解释为变量名,然后替换为变量的值。
|
||
</simpara>
|
||
<example>
|
||
<title>String Interpolation</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
$juice = "apple";
|
||
|
||
echo "He drank some $juice juice." . PHP_EOL;
|
||
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
He drank some apple juice.
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
|
||
<simpara>
|
||
从形式上讲,基本变量替换语法的结构如下:
|
||
</simpara>
|
||
<informalexample>
|
||
<programlisting>
|
||
<![CDATA[
|
||
string-variable::
|
||
variable-name (offset-or-property)?
|
||
| ${ expression }
|
||
|
||
offset-or-property::
|
||
offset-in-string
|
||
| property-in-string
|
||
|
||
offset-in-string::
|
||
[ name ]
|
||
| [ variable-name ]
|
||
| [ integer-literal ]
|
||
|
||
property-in-string::
|
||
-> name
|
||
|
||
variable-name::
|
||
$ name
|
||
|
||
name::
|
||
[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*
|
||
|
||
]]>
|
||
</programlisting>
|
||
</informalexample>
|
||
|
||
<warning>
|
||
<para>
|
||
<literal>${ expression }</literal> 语法从 PHP 8.2.0 开始被弃用,因为它可能被解释为
|
||
<link linkend="language.variables.variable">可变变量</link>:
|
||
<informalexample>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
const foo = 'bar';
|
||
$foo = 'foo';
|
||
$bar = 'bar';
|
||
var_dump("${foo}");
|
||
var_dump("${(foo)}");
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs.82;
|
||
<screen>
|
||
<![CDATA[
|
||
Deprecated: Using ${var} in strings is deprecated, use {$var} instead in file on line 6
|
||
|
||
Deprecated: Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead in file on line 9
|
||
string(3) "foo"
|
||
string(3) "bar"
|
||
]]>
|
||
</screen>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
string(3) "foo"
|
||
string(3) "bar"
|
||
]]>
|
||
</screen>
|
||
</informalexample>
|
||
<link linkend="language.types.string.parsing.advanced">高级</link> 字符串插值语法应该被使用。
|
||
</para>
|
||
</warning>
|
||
|
||
<note>
|
||
<simpara>
|
||
如果无法形成有效的变量名,美元符号会保持原样。
|
||
</simpara>
|
||
<informalexample>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
echo "No interpolation $ has happened\n";
|
||
echo "No interpolation $\n has happened\n";
|
||
echo "No interpolation $2 has happened\n";
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
No interpolation $ has happened
|
||
No interpolation $
|
||
has happened
|
||
No interpolation $2 has happened
|
||
]]>
|
||
</screen>
|
||
</informalexample>
|
||
</note>
|
||
|
||
<example>
|
||
<title>插值数组或属性的第一个维度值</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
$juices = array("apple", "orange", "string_key" => "purple");
|
||
|
||
echo "He drank some $juices[0] juice.";
|
||
echo PHP_EOL;
|
||
echo "He drank some $juices[1] juice.";
|
||
echo PHP_EOL;
|
||
echo "He drank some $juices[string_key] juice.";
|
||
echo PHP_EOL;
|
||
|
||
class A {
|
||
public $s = "string";
|
||
}
|
||
|
||
$o = new A();
|
||
|
||
echo "Object value: $o->s.";
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
He drank some apple juice.
|
||
He drank some orange juice.
|
||
He drank some purple juice.
|
||
Object value: string.
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
|
||
<note>
|
||
<simpara>
|
||
数组键必须是无引号的,因此不能用基本语法将常量作为键来引用。使用
|
||
<link linkend="language.types.string.parsing.advanced">高级</link>
|
||
语法代替。
|
||
</simpara>
|
||
</note>
|
||
|
||
<simpara>
|
||
从 PHP 7.1.0 起,还支持<emphasis>负</emphasis>数字索引。
|
||
</simpara>
|
||
|
||
<example><title>负数索引</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
$string = 'string';
|
||
echo "The character at index -2 is $string[-2].", PHP_EOL;
|
||
$string[-3] = 'o';
|
||
echo "Changing the character at index -3 to o gives $string.", PHP_EOL;
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
The character at index -2 is n.
|
||
Changing the character at index -3 to o gives strong.
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
|
||
<simpara>
|
||
对于更复杂的情况,可以使用 <link linkend="language.types.string.parsing.advanced">高级</link>
|
||
语法。
|
||
</simpara>
|
||
</sect4>
|
||
|
||
|
||
<sect4 xml:id="language.types.string.parsing.advanced">
|
||
<title>高级(大括号)语法</title>
|
||
|
||
<simpara>
|
||
高级语法允许使用任意访问器对<emphasis>变量</emphasis>进行插值。
|
||
</simpara>
|
||
|
||
<simpara>
|
||
任何标量变量、数组元素或对象属性(<modifier>static</modifier> 或非
|
||
<modifier>static</modifier>)都可以通过这种语法进行插值。表达式的写法和
|
||
在<type>string</type>之外的写法一样,然后用大括号<literal>{</literal>和
|
||
<literal>}</literal>包围。由于<literal>{</literal>不能被转义,所以这种语法只有在
|
||
紧跟在<literal>{</literal>后面的<literal>$</literal>才会被识别。要得到一个字面的
|
||
<literal>{$</literal>,需要用<literal>{\$</literal>。以下是一些例子:
|
||
</simpara>
|
||
|
||
<example>
|
||
<title>Curly Syntax</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
const DATA_KEY = 'const-key';
|
||
$great = 'fantastic';
|
||
$arr = [
|
||
'1',
|
||
'2',
|
||
'3',
|
||
[41, 42, 43],
|
||
'key' => 'Indexed value',
|
||
'const-key' => 'Key with minus sign',
|
||
'foo' => ['foo1', 'foo2', 'foo3']
|
||
];
|
||
|
||
// 无效,输出 This is { fantastic}
|
||
echo "This is { $great}";
|
||
|
||
// 有效,输出 This is fantastic
|
||
echo "This is {$great}";
|
||
|
||
class Square {
|
||
public $width;
|
||
|
||
public function __construct(int $width) { $this->width = $width; }
|
||
}
|
||
|
||
$square = new Square(5);
|
||
|
||
// 有效
|
||
echo "This square is {$square->width}00 centimeters wide.";
|
||
|
||
|
||
// 有效,引用 key 仅使用花括号语法时有效
|
||
echo "This works: {$arr['key']}";
|
||
|
||
|
||
// 有效
|
||
echo "This works: {$arr[3][2]}";
|
||
|
||
echo "This works: {$arr[DATA_KEY]}";
|
||
|
||
// 使用多维数组时,在字符串内部时,始终使用括号括住数组
|
||
echo "This works: {$arr['foo'][2]}";
|
||
|
||
echo "This works: {$obj->values[3]->name}";
|
||
|
||
echo "This works: {$obj->$staticProp}";
|
||
|
||
// 无效,输出 C:\directory\{fantastic}.txt
|
||
echo "C:\directory\{$great}.txt";
|
||
|
||
// 有效,输出 C:\directory\fantastic.txt
|
||
echo "C:\\directory\\{$great}.txt";
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
|
||
<note>
|
||
<simpara>
|
||
由于该语法允许任意表达式,因此可以在高级语法中使用
|
||
<link linkend="language.variables.variable">可变变量</link>。
|
||
</simpara>
|
||
</note>
|
||
</sect4>
|
||
</sect3>
|
||
|
||
<sect3 xml:id="language.types.string.substr">
|
||
<title>存取和修改字符串中的字符</title>
|
||
|
||
<para>
|
||
<type>string</type> 中的字符可以通过一个从 0 开始的下标,用类似
|
||
<type>array</type> 结构中的方括号包含对应的数字来访问和修改,比如
|
||
<varname>$str[42]</varname>。可以把 <type>string</type>
|
||
当成字符组成的 <type>array</type>。函数 <function>substr</function> 和
|
||
<function>substr_replace</function> 可用于操作多于一个字符的情况。
|
||
</para>
|
||
|
||
<note>
|
||
<simpara>
|
||
从 PHP 7.1.0 开始,还支持 string 负偏移量。从 string 尾部到指定位置的偏移量。
|
||
以前,负偏移量读取时(返回空 string)会发出 <constant>E_NOTICE</constant>,
|
||
写入时(string 保持不变)会发出 <constant>E_WARNING</constant>。
|
||
</simpara>
|
||
</note>
|
||
|
||
<note>
|
||
<simpara>
|
||
PHP 8.0.0 之前, 出于同样的目的,可以使用大括号访问 <type>string</type>,例如 <varname>$str{42}</varname>。
|
||
从 PHP 7.4.0 起,此大括号语法被弃用,自 PHP 8.0.0 开始不再受支持。
|
||
</simpara>
|
||
</note>
|
||
|
||
<warning>
|
||
<simpara>
|
||
用超出字符串长度的下标写入将会拉长该字符串并以空格填充。非整数类型下标会被转换成整数。非法下标类型会产生一个
|
||
<constant>E_WARNING</constant> 级别错误。
|
||
写入时只用到了赋值字符串的第一个字符。
|
||
PHP 7.1.0 开始,用空字符串赋值会导致 fatal 错误;在之前赋给的值是
|
||
NULL 字符。
|
||
</simpara>
|
||
</warning>
|
||
|
||
<warning>
|
||
<simpara>
|
||
PHP 的字符串在内部是字节组成的数组。因此用花括号访问或修改字符串对多字节字符集很不安全。仅应对单字节编码例如
|
||
ISO-8859-1 的字符串进行此类操作。
|
||
</simpara>
|
||
</warning>
|
||
|
||
<note>
|
||
<simpara>
|
||
从 PHP 7.1.0 开始,对空字符串应用空索引运算符会引发致命错误。
|
||
以前是空字符串会被静默转为数组。
|
||
</simpara>
|
||
</note>
|
||
|
||
<example>
|
||
<title>一些字符串示例</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
// 取得字符串的第一个字符
|
||
$str = 'This is a test.';
|
||
$first = $str[0];
|
||
var_dump($first);
|
||
|
||
// 取得字符串的第三个字符
|
||
$third = $str[2];
|
||
var_dump($third);
|
||
|
||
// 取得字符串的最后一个字符
|
||
$str = 'This is still a test.';
|
||
$last = $str[strlen($str)-1];
|
||
var_dump($last);
|
||
|
||
// 修改字符串的最后一个字符
|
||
$str = 'Look at the sea';
|
||
$str[strlen($str)-1] = 'e';
|
||
var_dump($str);
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
|
||
<para>
|
||
字符串下标必须为整数或可转换为整数的字符串,否则会发出警告。之前类似
|
||
<literal>"foo"</literal> 的下标会无声地转换成 <literal>0</literal>。
|
||
</para>
|
||
|
||
<example>
|
||
<title>字符串无效下标的例子</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
$str = 'abc';
|
||
|
||
$keys = [ '1', '1.0', 'x', '1x' ];
|
||
|
||
foreach ($keys as $keyToTry) {
|
||
var_dump(isset($str[$keyToTry]));
|
||
|
||
try {
|
||
var_dump($str[$keyToTry]);
|
||
} catch (TypeError $e) {
|
||
echo $e->getMessage(), PHP_EOL;
|
||
}
|
||
|
||
echo PHP_EOL;
|
||
}
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
bool(true)
|
||
string(1) "b"
|
||
|
||
bool(false)
|
||
Cannot access offset of type string on string
|
||
|
||
bool(false)
|
||
Cannot access offset of type string on string
|
||
|
||
bool(false)
|
||
|
||
Warning: Illegal string offset "1x" in Standard input code on line 10
|
||
string(1) "b"
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
|
||
<note>
|
||
<para>
|
||
用 <literal>[]</literal> 或 <literal>{}</literal>
|
||
访问任何其它类型(不包括数组或具有相应接口的对象实现)的变量只会无声地返回 &null;。
|
||
</para>
|
||
</note>
|
||
|
||
<note>
|
||
<para>
|
||
可以直接在字符串原型中用
|
||
<literal>[]</literal> 或 <literal>{}</literal> 访问字符。
|
||
</para>
|
||
</note>
|
||
|
||
<note>
|
||
<para>
|
||
PHP 7.4 中弃用在字符串字面量中使用 <literal>{}</literal> 来访问字符。
|
||
PHP 8.0 已移除。
|
||
</para>
|
||
</note>
|
||
</sect3>
|
||
</sect2><!-- end syntax -->
|
||
|
||
<sect2 xml:id="language.types.string.useful-funcs">
|
||
<title>有用的函数和运算符</title>
|
||
|
||
<para>
|
||
字符串可以用 '.'(点)运算符连接起来,注意
|
||
'+'(加号)运算符<emphasis>没有</emphasis>这个功能。更多信息参考<link linkend="language.operators.string">字符串运算符</link>。
|
||
</para>
|
||
|
||
<para>
|
||
对于 <type>string</type> 的操作有很多有用的函数。
|
||
</para>
|
||
|
||
<simpara>
|
||
可以参考<link linkend="ref.strings">字符串函数</link>了解大部分函数,高级的查找与替换功能可以参考
|
||
<link linkend="ref.pcre">Perl 兼容正则表达式函数</link>。
|
||
</simpara>
|
||
|
||
<simpara>
|
||
另外还有 <link linkend="ref.url">URL
|
||
字符串函数</link>,也有加密/解密字符串的函数(<link linkend="ref.sodium">Sodium</link> 和
|
||
<link linkend="ref.hash">Hash</link>)。
|
||
</simpara>
|
||
|
||
<simpara>
|
||
最后,可以参考<link linkend="ref.ctype">字符类型函数</link>。
|
||
</simpara>
|
||
</sect2>
|
||
|
||
<sect2 xml:id="language.types.string.casting">
|
||
<title>转换成字符串</title>
|
||
|
||
<para>
|
||
一个值可以通过在其前面加上 <literal>(string)</literal> 或用 <function>strval</function>
|
||
函数来转变成字符串。在一个需要字符串的表达式中,会自动转换为
|
||
<type>string</type>。这发生在当使用 <function>echo</function> 或 <function>print</function>
|
||
函数,或当变量与 <type>string</type> 进行比较的时候。<link
|
||
linkend="language.types">类型</link>和<link
|
||
linkend="language.types.type-juggling">类型转换</link>可以更好的解释下面的事情,也可参考函数
|
||
<function>settype</function>。
|
||
</para>
|
||
|
||
<para>
|
||
一个布尔值 <type>bool</type> 的 &true; 被转换成 <type>string</type> 的
|
||
<literal>"1"</literal>。<type>bool</type> 的 &false; 被转换成
|
||
<literal>""</literal>(空字符串)。这种转换可以在 <type>bool</type>
|
||
和 <type>string</type> 之间相互进行。
|
||
</para>
|
||
|
||
<para>
|
||
一个整数 <type>int</type> 或浮点数 <type>float</type> 被转换为数字的字面样式的
|
||
<type>string</type>(包括 <type>float</type>
|
||
中的指数部分)。使用指数计数法的浮点数(<literal>4.1E+6</literal>)也可转换。
|
||
</para>
|
||
|
||
<note>
|
||
<para>
|
||
PHP 8.0.0 起,十进制小数点字符都是一个句号(<literal>.</literal>)。
|
||
而在此之前的版本,在脚本的区域(category LC_NUMERIC)
|
||
中定义了十进制小数点字符。参见 <function>setlocale</function>。
|
||
</para>
|
||
</note>
|
||
|
||
<para>
|
||
数组 <type>array</type> 总是转换成字符串
|
||
<literal>"Array"</literal>,因此,<function>echo</function> 和
|
||
<function>print</function> 无法显示出该<type>数组</type>的内容。要显示某个单元,可以用
|
||
<literal>echo $arr['foo']</literal> 这种结构。要显示整个数组内容见下文。
|
||
</para>
|
||
|
||
<para>
|
||
必须使用魔术方法 <link linkend="language.oop5.magic">__toString</link>
|
||
才能将 <type>object</type> 转换为 <type>string</type>。
|
||
</para>
|
||
|
||
<para>
|
||
资源 <type>Resource</type> 总会被转变成 <literal>"Resource id #1" </literal>
|
||
这种结构的字符串,其中的 <literal>1</literal> 是 PHP
|
||
在运行时分配给该 <type>resource</type> 的资源数字。
|
||
While the exact structure of this string should not be relied on
|
||
and is subject to change, it will always be unique for a given resource
|
||
within the lifetime of a script being executed (ie a Web request or CLI
|
||
process) and won't be reused.
|
||
要得到一个
|
||
<type>resource</type> 的类型,可以用函数 <function>get_resource_type</function>。
|
||
</para>
|
||
|
||
<para>
|
||
&null; 总是被转变成空字符串。
|
||
</para>
|
||
|
||
<para>
|
||
如上面所说的,直接把 <type>array</type>,<type>object</type> 或 <type>resource</type>
|
||
转换成 <type>string</type> 不会得到除了其类型之外的任何有用信息。可以使用函数
|
||
<function>print_r</function> 和 <function>var_dump</function> 列出这些类型的内容。
|
||
</para>
|
||
|
||
<para>
|
||
大部分的 PHP 值可以转变成 <type>string</type> 来永久保存,这被称作串行化,可以用函数
|
||
<function>serialize</function> 来实现。
|
||
</para>
|
||
|
||
</sect2>
|
||
|
||
<sect2 xml:id="language.types.string.details">
|
||
|
||
<title>字符串类型详解</title>
|
||
|
||
<para>
|
||
PHP 中的 <type>string</type>
|
||
的实现方式是一个由字节组成的数组再加上一个整数指明缓冲区长度。并无如何将字节转换成字符的信息,由程序员来决定。字符串由什么值来组成并无限制;特别的,其值为
|
||
<literal>0</literal>(“NUL bytes”)的字节可以处于字符串任何位置(不过有几个函数,在本手册中被称为非“二进制安全”的,也许会把
|
||
NUL 字节之后的数据全都忽略)。
|
||
</para>
|
||
<para>
|
||
字符串类型的此特性解释了为什么 PHP 中没有单独的“byte”类型 -
|
||
已经用字符串来代替了。返回非文本值的函数 - 例如从网络套接字读取的任意数据
|
||
- 仍会返回字符串。
|
||
</para>
|
||
<para>
|
||
由于 PHP 并不特别指明字符串的编码,那字符串到底是怎样编码的呢?例如字符串
|
||
<literal>"á"</literal> 到底是等于
|
||
<literal>"\xE1"</literal>(ISO-8859-1),<literal>"\xC3\xA1"</literal>(UTF-8,C
|
||
form),<literal>"\x61\xCC\x81"</literal>(UTF-8,D
|
||
form)还是任何其它可能的表达呢?答案是字符串会被按照该脚本文件相同的编码方式来编码。因此如果一个脚本的编码是
|
||
ISO-8859-1,则其中的字符串也会被编码为 ISO-8859-1,以此类推。不过这并不适用于激活了 Zend Multibyte
|
||
时;此时脚本可以是以任何方式编码的(明确指定或被自动检测)然后被转换为某种内部编码,然后字符串将被用此方式编码。注意脚本的编码有一些约束(如果激活了 Zend Multibyte 则是其内部编码)-
|
||
这意味着此编码应该是 ASCII 的兼容超集,例如
|
||
UTF-8 或 ISO-8859-1。不过要注意,依赖状态的编码其中相同的字节值可以用于首字母和非首字母而转换状态,这可能会造成问题。
|
||
</para>
|
||
<para>
|
||
当然了,要做到有用,操作文本的函数必须假定字符串是如何编码的。不幸的是,PHP
|
||
关于此的函数有很多变种:
|
||
</para>
|
||
<itemizedlist>
|
||
<listitem>
|
||
<simpara>
|
||
某些函数假定字符串是以单字节编码的,但并不需要将字节解释为特定的字符。例如
|
||
<function>substr</function>,<function>strpos</function>,<function>strlen</function> 和
|
||
<function>strcmp</function>。理解这些函数的另一种方法是它们作用于内存缓冲区,即按照字节和字节下标操作。
|
||
</simpara>
|
||
</listitem>
|
||
<listitem>
|
||
<simpara>
|
||
某些函数被传递入了字符串的编码方式,也可能会假定默认无此信息。例如
|
||
<function>htmlentities</function> 和
|
||
<link linkend="book.mbstring">mbstring</link> 扩展中的大部分函数。
|
||
</simpara>
|
||
</listitem>
|
||
<listitem>
|
||
<simpara>
|
||
其它函数使用了当前区域(见 <function>setlocale</function>),但是逐字节操作。
|
||
</simpara>
|
||
</listitem>
|
||
<listitem>
|
||
<simpara>
|
||
最后一些函数会假定字符串是使用某特定编码的,通常是
|
||
UTF-8。<link linkend="book.intl">intl</link> 扩展和
|
||
<link linkend="book.pcre">PCRE</link>(上例中仅在使用了
|
||
<literal>u</literal> 修饰符时)扩展中的大部分函数都是这样。
|
||
</simpara>
|
||
</listitem>
|
||
</itemizedlist>
|
||
|
||
<para>
|
||
最后,要书写能够正确使用 Unicode 的程序依赖于很小心地避免那些可能会损坏数据的函数。要使用来自于
|
||
<link linkend="book.intl">intl</link> 和 <link linkend="book.mbstring">mbstring</link>
|
||
扩展的函数。不过使用能处理 Unicode 编码的函数只是个开始。不管用何种语言提供的函数,最基本的还是了解
|
||
Unicode 规格。例如一个程序如果假定只有大写和小写,那可是大错特错。
|
||
</para>
|
||
</sect2>
|
||
|
||
</sect1><!-- end string -->
|
||
|
||
<!-- Keep this comment at the end of the file
|
||
Local variables:
|
||
mode: sgml
|
||
sgml-omittag:t
|
||
sgml-shorttag:t
|
||
sgml-minimize-attributes:nil
|
||
sgml-always-quote-attributes:t
|
||
sgml-indent-step:1
|
||
sgml-indent-data:t
|
||
indent-tabs-mode:nil
|
||
sgml-parent-document:nil
|
||
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
||
sgml-exposed-tags:nil
|
||
sgml-local-catalogs:nil
|
||
sgml-local-ecat-files:nil
|
||
End:
|
||
vim600: syn=xml fen fdm=syntax fdl=2 si
|
||
vim: et tw=78 syn=sgml
|
||
vi: ts=1 sw=1
|
||
-->
|