📄 language.types.string.html
字号:
<p class="simpara"> See the <a href="ref.strings.html" class="link">string functions section</a> for general functions, and the <a href="ref.regex.html" class="link">regular expression functions</a> or the <a href="ref.pcre.html" class="link">Perl-compatible regular expression functions</a> for advanced find & replace functionality. </p> <p class="simpara"> There are also <a href="ref.url.html" class="link">functions for URL strings</a>, and functions to encrypt/decrypt strings (<a href="ref.mcrypt.html" class="link">mcrypt</a> and <a href="ref.mhash.html" class="link">mhash</a>). </p> <p class="simpara"> Finally, see also the <a href="ref.ctype.html" class="link">character type functions</a>. </p> </div> <div id="language.types.string.casting" class="sect2"> <h3 class="title">Converting to string</h3> <p class="para"> A value can be converted to a <a href="language.types.string.html" class="type string">string</a> using the <i>(string)</i> cast or the <a href="function.strval.html" class="function">strval()</a> function. <a href="language.types.string.html" class="type String">String</a> conversion is automatically done in the scope of an expression where a <a href="language.types.string.html" class="type string">string</a> is needed. This happens when using the <a href="function.echo.html" class="function">echo()</a> or <a href="function.print.html" class="function">print()</a> functions, or when a variable is compared to a <a href="language.types.string.html" class="type string">string</a>. The sections on <a href="language.types.html" class="link">Types</a> and <a href="language.types.type-juggling.html" class="link">Type Juggling</a> will make the following clearer. See also the <a href="function.settype.html" class="function">settype()</a> function. </p> <p class="para"> A <a href="language.types.boolean.html" class="type boolean">boolean</a> <b><tt>TRUE</tt></b> value is converted to the <a href="language.types.string.html" class="type string">string</a> <i>"1"</i>. <a href="language.types.boolean.html" class="type Boolean">Boolean</a> <b><tt>FALSE</tt></b> is converted to <i>""</i> (the empty string). This allows conversion back and forth between <a href="language.types.boolean.html" class="type boolean">boolean</a> and <a href="language.types.string.html" class="type string">string</a> values. </p> <p class="para"> An <a href="language.types.integer.html" class="type integer">integer</a> or <a href="language.types.float.html" class="type float">float</a> is converted to a <a href="language.types.string.html" class="type string">string</a> representing the number textually (including the exponent part for <a href="language.types.float.html" class="type float">float</a>s). Floating point numbers can be converted using exponential notation (<i>4.1E+6</i>). </p> <blockquote><p><b class="note">Note</b>: The decimal point character is defined in the script's locale (category LC_NUMERIC). See the <a href="function.setlocale.html" class="function">setlocale()</a> function. <br /> </p></blockquote> <p class="para"> <a href="language.types.array.html" class="type Array">Array</a>s are always converted to the <a href="language.types.string.html" class="type string">string</a> <i>"Array"</i>; because of this, <a href="function.echo.html" class="function">echo()</a> and <a href="function.print.html" class="function">print()</a> can not by themselves show the contents of an <a href="language.types.array.html" class="type array">array</a>. To view a single element, use a construction such as <i>echo $arr['foo']</i>. See below for tips on viewing the entire contents. </p> <p class="para"> <a href="language.types.object.html" class="type Object">Object</a>s in PHP 4 are always converted to the <a href="language.types.string.html" class="type string">string</a> <i>"Object"</i>. To print the values of object members for debugging reasons, read the paragraphs below. To get an object's class name, use the <a href="function.get-class.html" class="function">get_class()</a> function. As of PHP 5, the <a href="language.oop5.magic.html" class="link">__toString</a> method is used when applicable. </p> <p class="para"> <a href="language.types.resource.html" class="type Resource">Resource</a>s are always converted to <a href="language.types.string.html" class="type string">string</a>s with the structure <i>"Resource id #1"</i>, where <i>1</i> is the unique number assigned to the <a href="language.types.resource.html" class="type resource">resource</a> by PHP at runtime. Do not rely upon this structure; it is subject to change. To get a <a href="language.types.resource.html" class="type resource">resource</a>'s type, use the <a href="function.get-resource-type.html" class="function">get_resource_type()</a> function. </p> <p class="para"> <b><tt>NULL</tt></b> is always converted to an empty string. </p> <p class="para"> As stated above, directly converting an <a href="language.types.array.html" class="type array">array</a>, <a href="language.types.object.html" class="type object">object</a>, or <a href="language.types.resource.html" class="type resource">resource</a> to a <a href="language.types.string.html" class="type string">string</a> does not provide any useful information about the value beyond its type. See the functions <a href="function.print-r.html" class="function">print_r()</a> and <a href="function.var-dump.html" class="function">var_dump()</a> for more effective means of inspecting the contents of these types. </p> <p class="para"> Most PHP values can also be converted to <a href="language.types.string.html" class="type string">string</a>s for permanent storage. This method is called serialization, and is performed by the <a href="function.serialize.html" class="function">serialize()</a> function. If the PHP engine was built with <a href="ref.wddx.html" class="link">WDDX</a> support, PHP values can also be serialized as well-formed XML text. </p> </div> <div id="language.types.string.conversion" class="sect2"> <h3 class="title">String conversion to numbers</h3> <p class="simpara"> When a <a href="language.types.string.html" class="type string">string</a> is evaluated in a numeric context, the resulting value and type are determined as follows. </p> <p class="simpara"> The <a href="language.types.string.html" class="type string">string</a> will be evaluated as a <a href="language.types.float.html" class="type float">float</a> if it contains any of the characters '.', 'e', or 'E'. Otherwise, it will be evaluated as an <a href="language.types.integer.html" class="type integer">integer</a>. </p> <p class="para"> The value is given by the initial portion of the <a href="language.types.string.html" class="type string">string</a>. If the <a href="language.types.string.html" class="type string">string</a> starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits. </p> <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">1 </span><span style="color: #007700">+ </span><span style="color: #DD0000">"10.5"</span><span style="color: #007700">; </span><span style="color: #FF8000">// $foo is float (11.5)<br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">1 </span><span style="color: #007700">+ </span><span style="color: #DD0000">"-1.3e3"</span><span style="color: #007700">; </span><span style="color: #FF8000">// $foo is float (-1299)<br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">1 </span><span style="color: #007700">+ </span><span style="color: #DD0000">"bob-1.3e3"</span><span style="color: #007700">; </span><span style="color: #FF8000">// $foo is integer (1)<br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">1 </span><span style="color: #007700">+ </span><span style="color: #DD0000">"bob3"</span><span style="color: #007700">; </span><span style="color: #FF8000">// $foo is integer (1)<br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">1 </span><span style="color: #007700">+ </span><span style="color: #DD0000">"10 Small Pigs"</span><span style="color: #007700">; </span><span style="color: #FF8000">// $foo is integer (11)<br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">4 </span><span style="color: #007700">+ </span><span style="color: #DD0000">"10.2 Little Piggies"</span><span style="color: #007700">; </span><span style="color: #FF8000">// $foo is float (14.2)<br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #DD0000">"10.0 pigs " </span><span style="color: #007700">+ </span><span style="color: #0000BB">1</span><span style="color: #007700">; </span><span style="color: #FF8000">// $foo is float (11)<br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #DD0000">"10.0 pigs " </span><span style="color: #007700">+ </span><span style="color: #0000BB">1.0</span><span style="color: #007700">; </span><span style="color: #FF8000">// $foo is float (11) <br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <p class="simpara"> For more information on this conversion, see the Unix manual page for strtod(3). </p> <p class="para"> To test any of the examples in this section, cut and paste the examples and insert the following line to see what's going on: </p> <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"\$foo==$foo; type is " </span><span style="color: #007700">. </span><span style="color: #0000BB">gettype </span><span style="color: #007700">(</span><span style="color: #0000BB">$foo</span><span style="color: #007700">) . </span><span style="color: #DD0000">"<br />\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <p class="para"> Do not expect to get the code of one character by converting it to integer, as is done in C. Use the <a href="function.ord.html" class="function">ord()</a> and <a href="function.chr.html" class="function">chr()</a> functions to convert between ASCII codes and characters. </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.types.float.html">Floating point numbers</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.types.array.html">Arrays</a></div> <div class="up"><a href="language.types.html">Types</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -