Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > by-pkgid > 05cd670d8a02b2b4a0ffb1756f2e8308 > files > 12022

php-manual-zh-5.2.4-1mdv2008.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>字符串</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="PHP 手册"
HREF="index.html"><LINK
REL="UP"
TITLE="类型"
HREF="language.types.html"><LINK
REL="PREVIOUS"
TITLE="浮点型"
HREF="language.types.float.html"><LINK
REL="NEXT"
TITLE="数组"
HREF="language.types.array.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=UTF-8"></HEAD
><BODY
CLASS="sect1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>PHP 手册</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="language.types.float.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>章 11. 类型</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="language.types.array.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="language.types.string"
>字符串</A
></H1
><P
>&#13;   <A
HREF="language.types.string.html"
><B
CLASS="type"
>string</B
></A
> 是一系列字符。在 PHP 中,字符和字节一样,也就是说,一共有 256 种不同字符的可能性。这也暗示 PHP 对 Unicode 没有本地支持。请参阅函数 <A
HREF="function.utf8-encode.html"
><B
CLASS="function"
>utf8_encode()</B
></A
>
   和 <A
HREF="function.utf8-decode.html"
><B
CLASS="function"
>utf8_decode()</B
></A
> 以了解有关 Unicode 支持。
  </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
    一个字符串变得非常巨大也没有问题,PHP 没有给字符串的大小强加实现范围,所以完全没有理由担心长字符串。
   </P
></BLOCKQUOTE
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.types.string.syntax"
>语法</A
></H2
><P
>&#13;    字符串可以用三种字面上的方法定义。
    <P
></P
><UL
><LI
><P
>&#13;       <A
HREF="language.types.string.html#language.types.string.syntax.single"
>单引号</A
>
      </P
></LI
><LI
><P
>&#13;       <A
HREF="language.types.string.html#language.types.string.syntax.double"
>双引号</A
>
      </P
></LI
><LI
><P
>&#13;       <A
HREF="language.types.string.html#language.types.string.syntax.heredoc"
>定界符</A
>
      </P
></LI
></UL
>
   </P
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="language.types.string.syntax.single"
>单引号</A
></H3
><P
>&#13;     指定一个简单字符串的最简单的方法是用单引号(字符 <TT
CLASS="literal"
>'</TT
>)括起来。
    </P
><P
>&#13;     要表示一个单引号,需要用反斜线(<TT
CLASS="literal"
>\</TT
>)转义,和很多其它语言一样。如果在单引号之前或字符串结尾需要出现一个反斜线,需要用两个反斜线表示。注意如果试图转义任何其它字符,反斜线本身也会被显示出来!所以通常不需要转义反斜线本身。
     <DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
       在 PHP 3 中,此情况下将发出一个 <TT
CLASS="literal"
>E_NOTICE</TT
> 级的警告。
      </P
></BLOCKQUOTE
></DIV
>
     <DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
       和其他两种语法不同,单引号字符串中出现的<A
HREF="language.variables.html"
>变量</A
>和转义序列<SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>不会</I
></SPAN
>被变量的值替代。
      </P
></BLOCKQUOTE
></DIV
>
     <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN2984"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">echo </font><font color="#DD0000">'this is a simple string'</font><font color="#007700">;<br /><br />echo </font><font color="#DD0000">'You can also have embedded newlines in<br />strings this way as it is<br />okay to do'</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Outputs: Arnold once said: "I'll be back"<br /></font><font color="#007700">echo </font><font color="#DD0000">'Arnold once said: "I\'ll be back"'</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Outputs: You deleted C:\*.*?<br /></font><font color="#007700">echo </font><font color="#DD0000">'You deleted C:\\*.*?'</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Outputs: You deleted C:\*.*?<br /></font><font color="#007700">echo </font><font color="#DD0000">'You deleted C:\*.*?'</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Outputs: This will not expand: \n a newline<br /></font><font color="#007700">echo </font><font color="#DD0000">'This will not expand: \n a newline'</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Outputs: Variables do not $expand $either<br /></font><font color="#007700">echo </font><font color="#DD0000">'Variables do not $expand $either'</font><font color="#007700">;<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="language.types.string.syntax.double"
>双引号</A
></H3
><P
>&#13;     如果用双引号(")括起字符串,PHP 懂得更多特殊字符的转义序列:
    </P
><DIV
CLASS="table"
><A
NAME="AEN2989"
></A
><P
><B
>表 11-1. 转义字符</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL><THEAD
><TR
><TH
>序列</TH
><TH
>含义</TH
></TR
></THEAD
><TBODY
><TR
><TD
><TT
CLASS="literal"
>\n</TT
></TD
><TD
>换行(LF 或 ASCII 字符 0x0A(10))</TD
></TR
><TR
><TD
><TT
CLASS="literal"
>\r</TT
></TD
><TD
>回车(CR 或 ASCII 字符 0x0D(13))</TD
></TR
><TR
><TD
><TT
CLASS="literal"
>\t</TT
></TD
><TD
>水平制表符(HT 或 ASCII 字符 0x09(9))</TD
></TR
><TR
><TD
><TT
CLASS="literal"
>\\</TT
></TD
><TD
>反斜线</TD
></TR
><TR
><TD
><TT
CLASS="literal"
>\$</TT
></TD
><TD
>美元符号</TD
></TR
><TR
><TD
><TT
CLASS="literal"
>\"</TT
></TD
><TD
>双引号</TD
></TR
><TR
><TD
><TT
CLASS="literal"
>\[0-7]{1,3}</TT
></TD
><TD
>&#13;         此正则表达式序列匹配一个用八进制符号表示的字符
        </TD
></TR
><TR
><TD
><TT
CLASS="literal"
>\x[0-9A-Fa-f]{1,2}</TT
></TD
><TD
>&#13;         此正则表达式序列匹配一个用十六进制符号表示的字符
        </TD
></TR
></TBODY
></TABLE
></DIV
><P
>&#13;     此外,如果试图转义任何其它字符,反斜线本身也会被显示出来!在 PHP 5.1.1
     之前,<TT
CLASS="literal"
>\{$var}</TT
> 中的反斜线不会被显示出来。
    </P
><P
>&#13;     双引号字符串最重要的一点是其中的变量名会被变量值替代。细节参见<A
HREF="language.types.string.html#language.types.string.parsing"
>字符串解析</A
>。
    </P
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="language.types.string.syntax.heredoc"
>定界符</A
></H3
><P
>&#13;     另一种给字符串定界的方法使用定界符语法(“&#60;&#60;&#60;”)。应该在 <TT
CLASS="literal"
>&#60;&#60;&#60;</TT
> 之后提供一个标识符,然后是字符串,然后是同样的标识符结束字符串。
    </P
><P
>&#13;     结束标识符<SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>必须</I
></SPAN
>从行的第一列开始。同样,标识符也必须遵循 PHP 中其它任何标签的命名规则:只能包含字母数字下划线,而且必须以下划线或非数字字符开始。
    </P
><DIV
CLASS="warning"
><P
></P
><TABLE
CLASS="warning"
BORDER="1"
WIDTH="100%"
><TR
><TD
ALIGN="CENTER"
><B
>警告</B
></TD
></TR
><TR
><TD
ALIGN="LEFT"
><P
>&#13;      很重要的一点必须指出,结束标识符所在的行不能包含任何其它字符,<SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>可能</I
></SPAN
>除了一个分号(<TT
CLASS="literal"
>;</TT
>)之外。这尤其意味着该标识符<SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>不能被缩进</I
></SPAN
>,而且在分号之前和之后都不能有任何空格或制表符。同样重要的是要意识到在结束标识符之前的第一个字符必须是你的操作系统中定义的换行符。例如在
      Macintosh 系统中是 <TT
CLASS="literal"
>\r</TT
>。
     </P
><P
>&#13;      如果破坏了这条规则使得结束标识符不“干净”,则它不会被视为结束标识符,PHP
      将继续寻找下去。如果在这种情况下找不到合适的结束标识符,将会导致一个在脚本最后一行出现的语法错误。
     </P
><P
>&#13;      不能用定界符语法初始化类成员。用其它字符串语法替代。
      <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN3047"
></A
><P
><B
>例 11-3. 非法的例子</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">class </font><font color="#0000BB">foo </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">public $bar </font><font color="#007700">= &lt;&lt;&lt;EOT<br /></font><font color="#0000BB">bar<br /></font><font color="#007700">EOT;<br />}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
     </P
></TD
></TR
></TABLE
></DIV
><P
>&#13;     定界符文本表现的就和双引号字符串一样,只是没有双引号。这意味着在定界符文本中不需要转义引号,不过仍然可以用以上列出来的转义代码。变量会被展开,但当在定界符文本中表达复杂变量时和字符串一样同样也要注意。
     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN3051"
></A
><P
><B
>例 11-4. 定界符字符串例子</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$str </font><font color="#007700">= &lt;&lt;&lt;EOD<br /></font><font color="#0000BB">Example of string<br />spanning multiple lines<br />using heredoc syntax.<br /></font><font color="#007700">EOD;<br /><br /></font><font color="#FF8000">/* More complex example, with variables. */<br /></font><font color="#007700">class </font><font color="#0000BB">foo<br /></font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$foo</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$bar</font><font color="#007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">foo</font><font color="#007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">foo </font><font color="#007700">= </font><font color="#DD0000">'Foo'</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">bar </font><font color="#007700">= array(</font><font color="#DD0000">'Bar1'</font><font color="#007700">, </font><font color="#DD0000">'Bar2'</font><font color="#007700">, </font><font color="#DD0000">'Bar3'</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#0000BB">$foo </font><font color="#007700">= new </font><font color="#0000BB">foo</font><font color="#007700">();<br /></font><font color="#0000BB">$name </font><font color="#007700">= </font><font color="#DD0000">'MyName'</font><font color="#007700">;<br /><br />echo &lt;&lt;&lt;EOT<br /></font><font color="#0000BB">My name is "$name". I am printing some $foo</font><font color="#007700">-&gt;</font><font color="#0000BB">foo.<br />Now, I am printing some </font><font color="#007700">{</font><font color="#0000BB">$foo</font><font color="#007700">-&gt;</font><font color="#0000BB">bar</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">]}</font><font color="#0000BB">.<br />This should print a capital 'A': </font><font color="#007700">\x41</font><font color="#0000BB"><br /></font><font color="#007700">EOT;<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
      定界符支持是 PHP 4 中加入的。
     </P
></BLOCKQUOTE
></DIV
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="language.types.string.parsing"
>变量解析</A
></H3
><P
>&#13;     当用双引号或者定界符指定字符串时,其中的<A
HREF="language.variables.html"
>变量</A
>会被解析。
    </P
><P
>&#13;     有两种语法,一种<A
HREF="language.types.string.html#language.types.string.parsing.simple"
>简单的</A
>和一种<A
HREF="language.types.string.html#language.types.string.parsing.complex"
>复杂的</A
>。简单语法最通用和方便,它提供了解析变量,数组值,或者对象属性的方法。
    </P
><P
>&#13;     复杂语法是 PHP 4 引进的,可以用花括号括起一个表达式。
    </P
><DIV
CLASS="sect4"
><H4
CLASS="sect4"
><A
NAME="language.types.string.parsing.simple"
>简单语法</A
></H4
><P
>&#13;      如果遇到美元符号(<TT
CLASS="literal"
>$</TT
>),解析器会尽可能多地取得后面的字符以组成一个合法的变量名。如果想明示指定名字的结束,用花括号把变量名括起来。
     </P
><DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN3068"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$beer </font><font color="#007700">= </font><font color="#DD0000">'Heineken'</font><font color="#007700">;<br />echo </font><font color="#DD0000">"$beer's taste is great"</font><font color="#007700">; </font><font color="#FF8000">// works, "'" is an invalid character for varnames<br /></font><font color="#007700">echo </font><font color="#DD0000">"He drank some $beers"</font><font color="#007700">;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// won't work, 's' is a valid character for varnames<br /></font><font color="#007700">echo </font><font color="#DD0000">"He drank some </font><font color="#007700">${</font><font color="#DD0000">beer</font><font color="#007700">}</font><font color="#DD0000">s"</font><font color="#007700">; </font><font color="#FF8000">// works<br /></font><font color="#007700">echo </font><font color="#DD0000">"He drank some </font><font color="#007700">{</font><font color="#DD0000">$beer</font><font color="#007700">}</font><font color="#DD0000">s"</font><font color="#007700">; </font><font color="#FF8000">// works<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;      同样也可以解析数组索引或者对象属性。对于数组索引,右方括号(<TT
CLASS="literal"
>]</TT
>)标志着索引的结束。对象属性则和简单变量适用同样的规则,尽管对于对象属性没有像变量那样的小技巧。
      
     </P
><DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN3072"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">// These examples are specific to using arrays inside of strings.<br />// When outside of a string, always quote your array string keys<br />// and do not use {braces} when outside of strings either.<br /><br />// Let's show all errors<br /></font><font color="#0000BB">error_reporting</font><font color="#007700">(</font><font color="#0000BB">E_ALL</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$fruits </font><font color="#007700">= array(</font><font color="#DD0000">'strawberry' </font><font color="#007700">=&gt; </font><font color="#DD0000">'red'</font><font color="#007700">, </font><font color="#DD0000">'banana' </font><font color="#007700">=&gt; </font><font color="#DD0000">'yellow'</font><font color="#007700">);<br /></font><font color="#FF8000">// Works but note that this works differently outside string-quotes<br /></font><font color="#007700">echo </font><font color="#DD0000">"A banana is $fruits</font><font color="#007700">[</font><font color="#DD0000">banana</font><font color="#007700">]</font><font color="#DD0000">."</font><font color="#007700">;<br /></font><font color="#FF8000">// Works<br /></font><font color="#007700">echo </font><font color="#DD0000">"A banana is </font><font color="#007700">{</font><font color="#DD0000">$fruits</font><font color="#007700">[</font><font color="#DD0000">'banana'</font><font color="#007700">]}</font><font color="#DD0000">."</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Works but PHP looks for a constant named banana first<br />// as described below.<br /></font><font color="#007700">echo </font><font color="#DD0000">"A banana is </font><font color="#007700">{</font><font color="#DD0000">$fruits</font><font color="#007700">[</font><font color="#DD0000">banana</font><font color="#007700">]}</font><font color="#DD0000">."</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Won't work, use braces.&nbsp;&nbsp;This results in a parse error.<br /></font><font color="#007700">echo </font><font color="#DD0000">"A banana is $fruits</font><font color="#007700">[</font><font color="#DD0000">'banana'</font><font color="#007700">]</font><font color="#DD0000">."</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Works<br /></font><font color="#007700">echo </font><font color="#DD0000">"A banana is " </font><font color="#007700">. </font><font color="#0000BB">$fruits</font><font color="#007700">[</font><font color="#DD0000">'banana'</font><font color="#007700">] . </font><font color="#DD0000">"."</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Works<br /><br /></font><font color="#007700">echo </font><font color="#DD0000">"This square is $square</font><font color="#007700">-&gt;</font><font color="#DD0000">width meters broad."</font><font color="#007700">;<br /></font><font color="#FF8000">// Won't work. For a solution, see the complex syntax.<br /></font><font color="#007700">echo </font><font color="#DD0000">"This square is $square</font><font color="#007700">-&gt;</font><font color="#DD0000">width00 centimeters broad."</font><font color="#007700">;<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;      对于任何更复杂的情况,应该使用复杂语法。
     </P
></DIV
><DIV
CLASS="sect4"
><H4
CLASS="sect4"
><A
NAME="language.types.string.parsing.complex"
>复杂(花括号)语法</A
></H4
><P
>&#13;      不是因为语法复杂而称其为复杂,而是因为用此方法可以包含复杂的表达式。
     </P
><P
>&#13;      事实上,用此语法可以在字符串中包含任何在名字空间的值。仅仅用和在字符串之外同样的方法写一个表达式,然后用
      { 和 } 把它包含进来。因为不能转义“{”,此语法仅在 $ 紧跟在 {
      后面时被识别(用“{\$”来得到一个字面上的“{$”)。用一些例子可以更清晰:
     </P
><DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN3079"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">// Let's show all errors<br /></font><font color="#0000BB">error_reporting</font><font color="#007700">(</font><font color="#0000BB">E_ALL</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$great </font><font color="#007700">= </font><font color="#DD0000">'fantastic'</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// 不行,输出为:This is { fantastic}<br /></font><font color="#007700">echo </font><font color="#DD0000">"This is </font><font color="#007700">{</font><font color="#DD0000"> $great</font><font color="#007700">}</font><font color="#DD0000">"</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// 可以,输出为:This is fantastic<br /></font><font color="#007700">echo </font><font color="#DD0000">"This is </font><font color="#007700">{</font><font color="#DD0000">$great</font><font color="#007700">}</font><font color="#DD0000">"</font><font color="#007700">;<br />echo </font><font color="#DD0000">"This is </font><font color="#007700">${</font><font color="#DD0000">great</font><font color="#007700">}</font><font color="#DD0000">"</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Works<br /></font><font color="#007700">echo </font><font color="#DD0000">"This square is </font><font color="#007700">{</font><font color="#DD0000">$square</font><font color="#007700">-&gt;</font><font color="#DD0000">width</font><font color="#007700">}</font><font color="#DD0000">00 centimeters broad."</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Works<br /></font><font color="#007700">echo </font><font color="#DD0000">"This works: </font><font color="#007700">{</font><font color="#DD0000">$arr</font><font color="#007700">[</font><font color="#DD0000">4</font><font color="#007700">][</font><font color="#DD0000">3</font><font color="#007700">]}</font><font color="#DD0000">"</font><font color="#007700">;<br /></font><font color="#FF8000">// This is wrong for the same reason as $foo[bar] is wrong<br />// outside a string.&nbsp;&nbsp;In otherwords, it will still work but<br />// because PHP first looks for a constant named foo, it will<br />// throw an error of level E_NOTICE (undefined constant).<br /></font><font color="#007700">echo </font><font color="#DD0000">"This is wrong: </font><font color="#007700">{</font><font color="#DD0000">$arr</font><font color="#007700">[</font><font color="#DD0000">foo</font><font color="#007700">][</font><font color="#DD0000">3</font><font color="#007700">]}</font><font color="#DD0000">"</font><font color="#007700">;<br /></font><font color="#FF8000">// Works.&nbsp;&nbsp;When using multi-dimensional arrays, always use<br />// braces around arrays when inside of strings<br /></font><font color="#007700">echo </font><font color="#DD0000">"This works: </font><font color="#007700">{</font><font color="#DD0000">$arr</font><font color="#007700">[</font><font color="#DD0000">'foo'</font><font color="#007700">][</font><font color="#DD0000">3</font><font color="#007700">]}</font><font color="#DD0000">"</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Works.<br /></font><font color="#007700">echo </font><font color="#DD0000">"This works: " </font><font color="#007700">. </font><font color="#0000BB">$arr</font><font color="#007700">[</font><font color="#DD0000">'foo'</font><font color="#007700">][</font><font color="#0000BB">3</font><font color="#007700">];<br /><br />echo </font><font color="#DD0000">"You can even write </font><font color="#007700">{</font><font color="#DD0000">$obj</font><font color="#007700">-&gt;</font><font color="#DD0000">values</font><font color="#007700">[</font><font color="#DD0000">3</font><font color="#007700">]-&gt;</font><font color="#DD0000">name</font><font color="#007700">}</font><font color="#DD0000">"</font><font color="#007700">;<br /><br />echo </font><font color="#DD0000">"This is the value of the var named $name: </font><font color="#007700">{${</font><font color="#DD0000">$name</font><font color="#007700">}}</font><font color="#DD0000">"</font><font color="#007700">;<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
></DIV
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="language.types.string.substr"
>访问和修改字符串中的字符</A
></H3
><P
>&#13;     字符串中的字符可以通过在字符串之后用花括号指定所要字符从零开始的偏移量来访问和修改。
    </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
      为了向下兼容,仍然可以用方括号。不过此语法自 PHP 4 起已过时。
     </P
></BLOCKQUOTE
></DIV
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN3087"
></A
><P
><B
>例 11-5. 一些字符串例子</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">// Get the first character of a string<br /></font><font color="#0000BB">$str </font><font color="#007700">= </font><font color="#DD0000">'This is a test.'</font><font color="#007700">;<br /></font><font color="#0000BB">$first </font><font color="#007700">= </font><font color="#0000BB">$str</font><font color="#007700">{</font><font color="#0000BB">0</font><font color="#007700">};<br /></font><font color="#FF8000">// Get the third character of a string<br /></font><font color="#0000BB">$third </font><font color="#007700">= </font><font color="#0000BB">$str</font><font color="#007700">{</font><font color="#0000BB">2</font><font color="#007700">};<br /><br /></font><font color="#FF8000">// Get the last character of a string.<br /></font><font color="#0000BB">$str </font><font color="#007700">= </font><font color="#DD0000">'This is still a test.'</font><font color="#007700">;<br /></font><font color="#0000BB">$last </font><font color="#007700">= </font><font color="#0000BB">$str</font><font color="#007700">{</font><font color="#0000BB">strlen</font><font color="#007700">(</font><font color="#0000BB">$str</font><font color="#007700">)-</font><font color="#0000BB">1</font><font color="#007700">};<br /><br /></font><font color="#FF8000">// Modify the last character of a string<br /></font><font color="#0000BB">$str </font><font color="#007700">= </font><font color="#DD0000">'Look at the sea'</font><font color="#007700">;<br /></font><font color="#0000BB">$str</font><font color="#007700">{</font><font color="#0000BB">strlen</font><font color="#007700">(</font><font color="#0000BB">$str</font><font color="#007700">)-</font><font color="#0000BB">1</font><font color="#007700">} = </font><font color="#DD0000">'e'</font><font color="#007700">;<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
></DIV
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.types.string.useful-funcs"
>实用函数及运算符</A
></H2
><P
>&#13;    字符串可以用“.”(点)运算符连接。注意这里不能用“+”(加)运算符。更多信息参见<A
HREF="language.operators.string.html"
>字符串运算符</A
>。
   </P
><P
>&#13;    有很多实用函数来改变字符串。
   </P
><P
>&#13;    普通函数见<A
HREF="ref.strings.html"
>字符串函数</A
>一节,高级搜索和替换见正则表达式函数(两种风格:<A
HREF="ref.pcre.html"
>Perl</A
> 和 <A
HREF="ref.regex.html"
>POSIX 扩展</A
>)。
   </P
><P
>&#13;    还有 <A
HREF="ref.url.html"
>URL 字符串函数</A
>,以及加密/解密字符串的函数(<A
HREF="ref.mcrypt.html"
>mcrypt</A
> 和 <A
HREF="ref.mhash.html"
>mhash</A
>)。
   </P
><P
>&#13;    最后,如果还是找不到想要的函数,参见<A
HREF="ref.ctype.html"
>字符类型函数</A
>。
   </P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.types.string.casting"
>字符串转换</A
></H2
><P
>&#13;    可以用 <TT
CLASS="literal"
>(string)</TT
> 标记或者 <A
HREF="function.strval.html"
><B
CLASS="function"
>strval()</B
></A
>
    函数将一个值转换为字符串。当某表达式需要字符串时,字符串的转换会在表达式范围内自动完成。例如当使用
    <A
HREF="function.echo.html"
><B
CLASS="function"
>echo()</B
></A
> 或者 <A
HREF="function.print.html"
><B
CLASS="function"
>print()</B
></A
>
    函数时,或者将一个变量值与一个字符串进行比较的时候。阅读手册中有关<A
HREF="language.types.html"
>类型</A
>和<A
HREF="language.types.type-juggling.html"
>类型戏法</A
>中的部分有助于更清楚一些。参见
    <A
HREF="function.settype.html"
><B
CLASS="function"
>settype()</B
></A
>。
   </P
><P
>&#13;    布尔值 <TT
CLASS="constant"
><B
>TRUE</B
></TT
> 将被转换为字符串 <TT
CLASS="literal"
>"1"</TT
>,而值 <TT
CLASS="constant"
><B
>FALSE</B
></TT
> 将被表示为
    <TT
CLASS="literal"
>""</TT
>(即空字符串)。这样就可以随意地在布尔值和字符串之间进行比较。
   </P
><P
>&#13;    整数或浮点数数值在转换成字符串时,字符串由表示这些数值的数字字符组成(浮点数还包含有指数部分)。
   </P
><P
>&#13;    数组将被转换成字符串 <TT
CLASS="literal"
>"Array"</TT
>,因此无法通过
    <A
HREF="function.echo.html"
><B
CLASS="function"
>echo()</B
></A
> 或者 <A
HREF="function.print.html"
><B
CLASS="function"
>print()</B
></A
>
    函数来输出数组的内容。请参考下文以获取更多提示。
   </P
><P
>&#13;    对象将被转换成字符串
    <TT
CLASS="literal"
>"Object"</TT
>。如果因为调试需要,需要将对象的成员变量打印出来,请阅读下文。如果希望得到该对象所依附的类的名称,请使用函数
    <A
HREF="function.get-class.html"
><B
CLASS="function"
>get_class()</B
></A
>。自
    PHP 5 起,如果合适可以用 __toString() 方法。
   </P
><P
>&#13;    资源类型总是以 <TT
CLASS="literal"
>"Resource id #1"</TT
> 的格式被转换成字符串,其中
    <TT
CLASS="literal"
>1</TT
> 是 PHP
    在运行时给资源指定的唯一标识。如果希望获取资源的类型,请使用函数
    <A
HREF="function.get-resource-type.html"
><B
CLASS="function"
>get_resource_type()</B
></A
>。
   </P
><P
>&#13;    <TT
CLASS="constant"
><B
>NULL</B
></TT
> 将被转换成空字符串。
   </P
><P
>&#13;    正如以上所示,将数组、对象或者资源打印出来,并不能提供任何关于这些值本身的有用的信息。请参阅函数
    <A
HREF="function.print-r.html"
><B
CLASS="function"
>print_r()</B
></A
> 和 <A
HREF="function.var-dump.html"
><B
CLASS="function"
>var_dump()</B
></A
>,对于调试来说,这些是更好的打印值的方法。
   </P
><P
>&#13;    可以将 PHP 的值转换为字符串以永久地储存它们。这种方法被称为序列化,可以用函数
    <A
HREF="function.serialize.html"
><B
CLASS="function"
>serialize()</B
></A
> 来完成该操作。如果在安装 PHP 时建立了
    <A
HREF="ref.wddx.html"
>WDDX</A
> 支持,还可以将 PHP 的值序列化为 XML 结构。
   </P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.types.string.conversion"
>字符串转换为数值</A
></H2
><P
>&#13;    当一个字符串被当作数字来求值时,根据以下规则来决定结果的类型和值。
   </P
><P
>&#13;    如果包括“.”,“e”或“E”其中任何一个字符的话,字符串被当作
    <A
HREF="language.types.float.html"
><B
CLASS="type"
>float</B
></A
> 来求值。否则就被当作整数。
   </P
><P
>&#13;    该值由字符串最前面的部分决定。如果字符串以合法的数字数据开始,就用该数字作为其值,否则其值为
    0(零)。合法数字数据由可选的正负号开始,后面跟着一个或多个数字(可选地包括十进制分数),后面跟着可选的指数。指数是一个“e”或者“E”后面跟着一个或多个数字。
   </P
><DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN3146"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$foo </font><font color="#007700">= </font><font color="#0000BB">1 </font><font color="#007700">+ </font><font color="#DD0000">"10.5"</font><font color="#007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// $foo is float (11.5)<br /></font><font color="#0000BB">$foo </font><font color="#007700">= </font><font color="#0000BB">1 </font><font color="#007700">+ </font><font color="#DD0000">"-1.3e3"</font><font color="#007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// $foo is float (-1299)<br /></font><font color="#0000BB">$foo </font><font color="#007700">= </font><font color="#0000BB">1 </font><font color="#007700">+ </font><font color="#DD0000">"bob-1.3e3"</font><font color="#007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// $foo is integer (1)<br /></font><font color="#0000BB">$foo </font><font color="#007700">= </font><font color="#0000BB">1 </font><font color="#007700">+ </font><font color="#DD0000">"bob3"</font><font color="#007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// $foo is integer (1)<br /></font><font color="#0000BB">$foo </font><font color="#007700">= </font><font color="#0000BB">1 </font><font color="#007700">+ </font><font color="#DD0000">"10 Small Pigs"</font><font color="#007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// $foo is integer (11)<br /></font><font color="#0000BB">$foo </font><font color="#007700">= </font><font color="#0000BB">4 </font><font color="#007700">+ </font><font color="#DD0000">"10.2 Little Piggies"</font><font color="#007700">; </font><font color="#FF8000">// $foo is float (14.2)<br /></font><font color="#0000BB">$foo </font><font color="#007700">= </font><font color="#DD0000">"10.0 pigs " </font><font color="#007700">+ </font><font color="#0000BB">1</font><font color="#007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// $foo is float (11)<br /></font><font color="#0000BB">$foo </font><font color="#007700">= </font><font color="#DD0000">"10.0 pigs " </font><font color="#007700">+ </font><font color="#0000BB">1.0</font><font color="#007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// $foo is float (11)<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;    此转换的更多信息见 Unix 手册中关于 strtod(3) 的部分。
   </P
><P
>&#13;    如果想测试本节中的任何例子,可以拷贝和粘贴这些例子并且加上下面这一行自己看看会发生什么:
    <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN3150"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">echo </font><font color="#DD0000">"</font><font color="#007700">\$</font><font color="#DD0000">foo==$foo; type is " </font><font color="#007700">. </font><font color="#0000BB">gettype </font><font color="#007700">(</font><font color="#0000BB">$foo</font><font color="#007700">) . </font><font color="#DD0000">"&lt;br /&gt;\n"</font><font color="#007700">;<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
   </P
><P
>&#13;    不要指望在将一个字符转换成整型时能够得到该字符的编码(可能也会在
    C 中这么做)。如果希望在字符编码和字符之间转换,请使用
    <A
HREF="function.ord.html"
><B
CLASS="function"
>ord()</B
></A
> 和 <A
HREF="function.chr.html"
><B
CLASS="function"
>chr()</B
></A
> 函数。
   </P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="language.types.float.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>起始页</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="language.types.array.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>浮点型</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="language.types.html"
ACCESSKEY="U"
>上一级</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>数组</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>