📄 language.types.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Types</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.basic-syntax.comments.html">Comments</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.types.boolean.html">Booleans</a></div> <div class="up"><a href="langref.html">Language Reference</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div> <h1>Types</h1><h2>Table of Contents</h2><ul class="chunklist chunklist_chapter"><li><a href="language.types.boolean.html">Booleans</a></li><li><a href="language.types.integer.html">Integers</a></li><li><a href="language.types.float.html">Floating point numbers</a></li><li><a href="language.types.string.html">Strings</a></li><li><a href="language.types.array.html">Arrays</a></li><li><a href="language.types.object.html">Objects</a></li><li><a href="language.types.resource.html">Resources</a></li><li><a href="language.types.null.html">NULL</a></li><li><a href="language.pseudo-types.html">Pseudo-types and variables used in this documentation</a></li><li><a href="language.types.type-juggling.html">Type Juggling</a></li></ul> <div id="language.types.intro" class="sect1"> <h2 class="title">Introduction</h2> <p class="simpara"> PHP supports eight primitive types. </p> <p class="para"> Four scalar types: </p> <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> <a href="language.types.boolean.html" class="type boolean">boolean</a> </span> </li> <li class="listitem"> <span class="simpara"> <a href="language.types.integer.html" class="type integer">integer</a> </span> </li> <li class="listitem"> <span class="simpara"> <a href="language.types.float.html" class="type float">float</a> (floating-point number, aka <a href="language.types.float.html" class="type double">double</a>) </span> </li> <li class="listitem"> <span class="simpara"> <a href="language.types.string.html" class="type string">string</a> </span> </li> </ul> <p class="para"> Two compound types: </p> <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> <a href="language.types.array.html" class="type array">array</a> </span> </li> <li class="listitem"> <span class="simpara"> <a href="language.types.object.html" class="type object">object</a> </span> </li> </ul> <p class="para"> And finally two special types: </p> <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> <a href="language.types.resource.html" class="type resource">resource</a> </span> </li> <li class="listitem"> <span class="simpara"> <a href="language.types.null.html" class="type NULL">NULL</a> </span> </li> </ul> <p class="para"> This manual also introduces some <a href="language.pseudo-types.html" class="link">pseudo-types</a> for readability reasons: </p> <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> <a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a> </span> </li> <li class="listitem"> <span class="simpara"> <a href="language.pseudo-types.html#language.types.number" class="type number">number</a> </span> </li> <li class="listitem"> <span class="simpara"> <a href="language.pseudo-types.html#language.types.callback" class="type callback">callback</a> </span> </li> </ul> <p class="para"> And the pseudo-variable <i><tt class="parameter">$...</tt></i>. </p> <p class="simpara"> Some references to the type "double" may remain in the manual. Consider double the same as float; the two names exist only for historic reasons. </p> <p class="simpara"> The type of a variable is not usually set by the programmer; rather, it is decided at runtime by PHP depending on the context in which that variable is used. </p> <blockquote><p><b class="note">Note</b>: <span class="simpara"> To check the type and value of an <a href="language.expressions.html" class="link">expression</a>, use the <a href="function.var-dump.html" class="function">var_dump()</a> function. </span> To get a human-readable representation of a type for debugging, use the <a href="function.gettype.html" class="function">gettype()</a> function. To check for a certain type, do <em class="emphasis">not</em> use <a href="function.gettype.html" class="function">gettype()</a>, but rather the <i>is_<span class="replaceable">type</span></i> functions. Some examples: <br /> <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$a_bool </span><span style="color: #007700">= </span><span style="color: #0000BB">TRUE</span><span style="color: #007700">; </span><span style="color: #FF8000">// a boolean<br /></span><span style="color: #0000BB">$a_str </span><span style="color: #007700">= </span><span style="color: #DD0000">"foo"</span><span style="color: #007700">; </span><span style="color: #FF8000">// a string<br /></span><span style="color: #0000BB">$a_str2 </span><span style="color: #007700">= </span><span style="color: #DD0000">'foo'</span><span style="color: #007700">; </span><span style="color: #FF8000">// a string<br /></span><span style="color: #0000BB">$an_int </span><span style="color: #007700">= </span><span style="color: #0000BB">12</span><span style="color: #007700">; </span><span style="color: #FF8000">// an integer<br /><br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">gettype</span><span style="color: #007700">(</span><span style="color: #0000BB">$a_bool</span><span style="color: #007700">); </span><span style="color: #FF8000">// prints out: boolean<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">gettype</span><span style="color: #007700">(</span><span style="color: #0000BB">$a_str</span><span style="color: #007700">); </span><span style="color: #FF8000">// prints out: string<br /><br />// If this is an integer, increment it by four<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">is_int</span><span style="color: #007700">(</span><span style="color: #0000BB">$an_int</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">$an_int </span><span style="color: #007700">+= </span><span style="color: #0000BB">4</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// If $bool is a string, print it out<br />// (does not print out anything)<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">is_string</span><span style="color: #007700">(</span><span style="color: #0000BB">$a_bool</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"String: $a_bool"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p></blockquote> <p class="simpara"> To forcibly convert a variable to a certain type, either <a href="language.types.type-juggling.html#language.types.typecasting" class="link">cast</a> the variable or use the <a href="function.settype.html" class="function">settype()</a> function on it. </p> <p class="simpara"> Note that a variable may be evaluated with different values in certain situations, depending on what type it is at the time. For more information, see the section on <a href="language.types.type-juggling.html" class="link">Type Juggling</a>. <a href="types.comparisons.html" class="link">The type comparison tables</a> may also be useful, as they show examples of various type-related comparisons. </p> </div> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.basic-syntax.comments.html">Comments</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.types.boolean.html">Booleans</a></div> <div class="up"><a href="langref.html">Language Reference</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 + -