📄 language.types.integer.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Integers</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.types.boolean.html">Booleans</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.types.float.html">Floating point numbers</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><hr /><div id="language.types.integer" class="sect1"> <h2 class="title">Integers</h2> <p class="simpara"> An <a href="language.types.integer.html" class="type integer">integer</a> is a number of the set Z = {..., -2, -1, 0, 1, 2, ...}. </p> <p class="para"> See also: </p> <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> <a href="ref.gmp.html" class="link">Arbitrary length integer / GMP</a> </span> </li> <li class="listitem"> <span class="simpara"> <a href="language.types.float.html" class="link">Floating point numbers</a> </span> </li> <li class="listitem"> <span class="simpara"> <a href="ref.bc.html" class="link">Arbitrary precision / BCMath</a> </span> </li> </ul> <div id="language.types.integer.syntax" class="sect2"> <h3 class="title">Syntax</h3> <p class="simpara"> <a href="language.types.integer.html" class="type Integer">Integer</a>s can be specified in decimal (base 10), hexadecimal (base 16), or octal (base 8) notation, optionally preceded by a sign (- or +). </p> <p class="para"> To use octal notation, precede the number with a <i>0</i> (zero). To use hexadecimal notation precede the number with <i>0x</i>. </p> <div class="example"> <p><b>Example #1 Integer literals</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$a </span><span style="color: #007700">= </span><span style="color: #0000BB">1234</span><span style="color: #007700">; </span><span style="color: #FF8000">// decimal number<br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= -</span><span style="color: #0000BB">123</span><span style="color: #007700">; </span><span style="color: #FF8000">// a negative number<br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= </span><span style="color: #0000BB">0123</span><span style="color: #007700">; </span><span style="color: #FF8000">// octal number (equivalent to 83 decimal)<br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= </span><span style="color: #0000BB">0x1A</span><span style="color: #007700">; </span><span style="color: #FF8000">// hexadecimal number (equivalent to 26 decimal)<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <p class="para"> Formally, the structure for <a href="language.types.integer.html" class="type integer">integer</a> literals is: </p> <div class="informalexample"> <div class="example-contents"><div class="cdata"><pre>decimal : [1-9][0-9]* | 0hexadecimal : 0[xX][0-9a-fA-F]+octal : 0[0-7]+integer : [+-]?decimal | [+-]?hexadecimal | [+-]?octal</pre></div> </div> </div> <p class="para"> The size of an <a href="language.types.integer.html" class="type integer">integer</a> is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). PHP does not support unsigned <a href="language.types.integer.html" class="type integer">integer</a>s. <a href="language.types.integer.html" class="type Integer">Integer</a> size can be determined using the constant <b><tt>PHP_INT_SIZE</tt></b>, and maximum value using the constant <b><tt>PHP_INT_MAX</tt></b> since PHP 4.4.0 and PHP 5.0.5. </p> <div class="warning"><b class="warning">Warning</b> <p class="para"> If an invalid digit is given in an octal <a href="language.types.integer.html" class="type integer">integer</a> (i.e. 8 or 9), the rest of the number is ignored. </p> <div class="example"> <p><b>Example #2 Octal weirdness</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">01090</span><span style="color: #007700">); </span><span style="color: #FF8000">// 010 octal = 8 decimal<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </div> </div> <div id="language.types.integer.overflow" class="sect2"> <h3 class="title">Integer overflow</h3> <p class="para"> If PHP encounters a number beyond the bounds of the <a href="language.types.integer.html" class="type integer">integer</a> type, it will be interpreted as a <a href="language.types.float.html" class="type float">float</a> instead. Also, an operation which results in a number beyond the bounds of the <a href="language.types.integer.html" class="type integer">integer</a> type will return a <a href="language.types.float.html" class="type float">float</a> instead. </p> <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$large_number </span><span style="color: #007700">= </span><span style="color: #0000BB">2147483647</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$large_number</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// output: int(2147483647)<br /><br /></span><span style="color: #0000BB">$large_number </span><span style="color: #007700">= </span><span style="color: #0000BB">2147483648</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$large_number</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// output: float(2147483648)<br /><br />// it's true also for hexadecimal specified integers between 2^31 and 2^32-1:<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">( </span><span style="color: #0000BB">0xffffffff </span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// output: float(4294967295)<br /><br />// this doesn't go for hexadecimal specified integers above 2^32-1:<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">( </span><span style="color: #0000BB">0x100000000 </span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// output: int(2147483647)<br /><br /></span><span style="color: #0000BB">$million </span><span style="color: #007700">= </span><span style="color: #0000BB">1000000</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$large_number </span><span style="color: #007700">= </span><span style="color: #0000BB">50000 </span><span style="color: #007700">* </span><span style="color: #0000BB">$million</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$large_number</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// output: float(50000000000)<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <div class="warning"><b class="warning">Warning</b> <p class="simpara"> Unfortunately, there was a bug in PHP which caused this to not always work correctly when negative numbers were involved. For example, the result of <i>-50000 * $million</i> is <i>-429496728</i>. However, when both operands were positive, there was no problem. </p> <p class="simpara"> This was fixed in PHP 4.1.0. </p> </div> <p class="para"> There is no <a href="language.types.integer.html" class="type integer">integer</a> division operator in PHP. <i>1/2</i> yields the <a href="language.types.float.html" class="type float">float</a> <i>0.5</i>. The value can be casted to an <a href="language.types.integer.html" class="type integer">integer</a> to round it downwards, or the <a href="function.round.html" class="function">round()</a> function provides finer control over rounding. </p> <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">25</span><span style="color: #007700">/</span><span style="color: #0000BB">7</span><span style="color: #007700">); </span><span style="color: #FF8000">// float(3.5714285714286) <br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((int) (</span><span style="color: #0000BB">25</span><span style="color: #007700">/</span><span style="color: #0000BB">7</span><span style="color: #007700">)); </span><span style="color: #FF8000">// int(3)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">25</span><span style="color: #007700">/</span><span style="color: #0000BB">7</span><span style="color: #007700">)); </span><span style="color: #FF8000">// float(4) <br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </div> <div id="language.types.integer.casting" class="sect2"> <h3 class="title">Converting to integer</h3> <p class="simpara"> To explicitly convert a value to <a href="language.types.integer.html" class="type integer">integer</a>, use either the <i>(int)</i> or <i>(integer)</i> casts. However, in most cases the cast is not needed, since a value will be automatically converted if an operator, function or control structure requires an <a href="language.types.integer.html" class="type integer">integer</a> argument. A value can also be converted to <a href="language.types.integer.html" class="type integer">integer</a> with the <a href="function.intval.html" class="function">intval()</a> function. </p> <p class="simpara"> See also: <a href="language.types.type-juggling.html" class="link">type-juggling</a>. </p> <div id="language.types.integer.casting.from-boolean" class="sect3"> <h4 class="title">From <a href="language.types.boolean.html" class="link">booleans</a></h4> <p class="simpara"> <b><tt>FALSE</tt></b> will yield <i>0</i> (zero), and <b><tt>TRUE</tt></b> will yield <i>1</i> (one). </p> </div> <div id="language.types.integer.casting.from-float" class="sect3"> <h4 class="title"> From <a href="language.types.float.html" class="link">floating point numbers</a> </h4> <p class="simpara"> When converting from <a href="language.types.float.html" class="type float">float</a> to <a href="language.types.integer.html" class="type integer">integer</a>, the number will be rounded <em class="emphasis">towards zero</em>. </p> <p class="para"> If the float is beyond the boundaries of <a href="language.types.integer.html" class="type integer">integer</a> (usually <i>+/- 2.15e+9 = 2^31</i>), the result is undefined, since the <a href="language.types.float.html" class="type float">float</a> doesn't have enough precision to give an exact <a href="language.types.integer.html" class="type integer">integer</a> result. No warning, not even a notice will be issued when this happens! </p> <div class="warning"><b class="warning">Warning</b> <p class="para"> Never cast an unknown fraction to <a href="language.types.integer.html" class="type integer">integer</a>, as this can sometimes lead to unexpected results. </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 (int) ( (</span><span style="color: #0000BB">0.1</span><span style="color: #007700">+</span><span style="color: #0000BB">0.7</span><span style="color: #007700">) * </span><span style="color: #0000BB">10 </span><span style="color: #007700">); </span><span style="color: #FF8000">// echoes 7!<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <p class="para"> See also the <a href="language.types.float.html#warn.float-precision" class="link">warning about float precision</a>. </p> </div> </div> <div id="language.types.integer.casting.from-string" class="sect3"> <h4 class="title">From strings</h4> <p class="simpara"> See <a href="language.types.string.html#language.types.string.conversion" class="link">String conversion to numbers</a> </p> </div> <div id="language.types.integer.casting.from-other" class="sect3"> <h4 class="title">From other types</h4> <div class="caution"><b class="caution">Caution</b> <p class="simpara"> The behaviour of converting to <a href="language.types.integer.html" class="type integer">integer</a> is undefined for other types. Do <em class="emphasis">not</em> rely on any observed behaviour, as it can change without notice. </p> </div> </div> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.types.boolean.html">Booleans</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.types.float.html">Floating point numbers</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 + -