⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 language.types.string.html

📁 php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容
💻 HTML
📖 第 1 页 / 共 4 页
字号:
  <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 &amp; 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>&quot;1&quot;</i>. <a href="language.types.boolean.html" class="type Boolean">Boolean</a> <b><tt>FALSE</tt></b> is converted to   <i>&quot;&quot;</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&#039;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>&quot;Array&quot;</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[&#039;foo&#039;]</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>&quot;Object&quot;</i>. To print the values of object members for   debugging reasons, read the paragraphs below. To get an object&#039;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>&quot;Resource id #1&quot;</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>&#039;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 &#039;.&#039;, &#039;e&#039;, or &#039;E&#039;. 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 &#039;e&#039; or   &#039;E&#039; 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">&lt;?php<br />$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #DD0000">"10.5"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;is&nbsp;float&nbsp;(11.5)<br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #DD0000">"-1.3e3"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;is&nbsp;float&nbsp;(-1299)<br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #DD0000">"bob-1.3e3"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;is&nbsp;integer&nbsp;(1)<br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #DD0000">"bob3"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;is&nbsp;integer&nbsp;(1)<br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #DD0000">"10&nbsp;Small&nbsp;Pigs"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;is&nbsp;integer&nbsp;(11)<br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">4&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #DD0000">"10.2&nbsp;Little&nbsp;Piggies"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;is&nbsp;float&nbsp;(14.2)<br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"10.0&nbsp;pigs&nbsp;"&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;is&nbsp;float&nbsp;(11)<br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"10.0&nbsp;pigs&nbsp;"&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">1.0</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;is&nbsp;float&nbsp;(11)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #0000BB">?&gt;</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&#039;s going on:  </p>  <div class="informalexample">   <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"\$foo==$foo;&nbsp;type&nbsp;is&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">gettype&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$foo</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</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 + -