<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Get the integer value of a variable</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.import-request-variables.html">import_request_variables</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.is-array.html">is_array</a></div> <div class="up"><a href="ref.var.html">Variable handling Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.intval" class="refentry"> <div class="refnamediv"> <h1 class="refname">intval</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">intval</span> — <span class="dc-title">Get the integer value of a variable</span></p> </div> <div class="refsect1 description" id="refsect1-function.intval-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><strong>intval</strong></span> ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$var</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$base</code><span class="initializer"> = 10</span></span> ] )</div> <p class="para rdfs-comment"> Returns the <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> value of <em><code class="parameter">var</code></em>, using the specified <em><code class="parameter">base</code></em> for the conversion (the default is base 10). <span class="function"><strong>intval()</strong></span> should not be used on objects, as doing so will emit an <strong><code>E_NOTICE</code></strong> level error and return 1. </p> </div> <div class="refsect1 parameters" id="refsect1-function.intval-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">var</code></em></span> <dd> <p class="para"> The scalar value being converted to an integer </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">base</code></em></span> <dd> <p class="para"> The base for the conversion </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> If <em><code class="parameter">base</code></em> is 0, the base used is determined by the format of <em><code class="parameter">var</code></em>: <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> if string includes a "0x" (or "0X") prefix, the base is taken as 16 (hex); otherwise, </span> </li> <li class="listitem"> <span class="simpara"> if string starts with "0", the base is taken as 8 (octal); otherwise, </span> </li> <li class="listitem"> <span class="simpara"> the base is taken as 10 (decimal). </span> </li> </ul> </p> </p></blockquote> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.intval-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> The integer value of <em><code class="parameter">var</code></em> on success, or 0 on failure. Empty arrays return 0, non-empty arrays return 1. </p> <p class="para"> The maximum value depends on the system. 32 bit systems have a maximum signed integer range of -2147483648 to 2147483647. So for example on such a system, <em>intval('1000000000000')</em> will return 2147483647. The maximum signed integer value for 64 bit systems is 9223372036854775807. </p> <p class="para"> Strings will most likely return 0 although this depends on the leftmost characters of the string. The common rules of <a href="language.types.integer.html#language.types.integer.casting" class="link">integer casting</a> apply. </p> </div> <div class="refsect1 examples" id="refsect1-function.intval-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-5255"> <p><strong>Example #1 <span class="function"><strong>intval()</strong></span> examples</strong></p> <div class="example-contents"><p> The following examples are based on a 32 bit system. </p></div> <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: #0000BB">intval</span><span style="color: #007700">(</span><span style="color: #0000BB">42</span><span style="color: #007700">); </span><span style="color: #FF8000">// 42<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">intval</span><span style="color: #007700">(</span><span style="color: #0000BB">4.2</span><span style="color: #007700">); </span><span style="color: #FF8000">// 4<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">intval</span><span style="color: #007700">(</span><span style="color: #DD0000">'42'</span><span style="color: #007700">); </span><span style="color: #FF8000">// 42<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">intval</span><span style="color: #007700">(</span><span style="color: #DD0000">'+42'</span><span style="color: #007700">); </span><span style="color: #FF8000">// 42<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">intval</span><span style="color: #007700">(</span><span style="color: #DD0000">'-42'</span><span style="color: #007700">); </span><span style="color: #FF8000">// -42<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">intval</span><span style="color: #007700">(</span><span style="color: #0000BB">042</span><span style="color: #007700">); </span><span style="color: #FF8000">// 34<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">intval</span><span style="color: #007700">(</span><span style="color: #DD0000">'042'</span><span style="color: #007700">); </span><span style="color: #FF8000">// 42<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">intval</span><span style="color: #007700">(</span><span style="color: #0000BB">1e10</span><span style="color: #007700">); </span><span style="color: #FF8000">// 1410065408<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">intval</span><span style="color: #007700">(</span><span style="color: #DD0000">'1e10'</span><span style="color: #007700">); </span><span style="color: #FF8000">// 1<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">intval</span><span style="color: #007700">(</span><span style="color: #0000BB">0x1A</span><span style="color: #007700">); </span><span style="color: #FF8000">// 26<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">intval</span><span style="color: #007700">(</span><span style="color: #0000BB">42000000</span><span style="color: #007700">); </span><span style="color: #FF8000">// 42000000<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">intval</span><span style="color: #007700">(</span><span style="color: #0000BB">420000000000000000000</span><span style="color: #007700">); </span><span style="color: #FF8000">// 0<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">intval</span><span style="color: #007700">(</span><span style="color: #DD0000">'420000000000000000000'</span><span style="color: #007700">); </span><span style="color: #FF8000">// 2147483647<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">intval</span><span style="color: #007700">(</span><span style="color: #0000BB">42</span><span style="color: #007700">, </span><span style="color: #0000BB">8</span><span style="color: #007700">); </span><span style="color: #FF8000">// 42<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">intval</span><span style="color: #007700">(</span><span style="color: #DD0000">'42'</span><span style="color: #007700">, </span><span style="color: #0000BB">8</span><span style="color: #007700">); </span><span style="color: #FF8000">// 34<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">intval</span><span style="color: #007700">(array()); </span><span style="color: #FF8000">// 0<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">intval</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">, </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">)); </span><span style="color: #FF8000">// 1<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.intval-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> The <em><code class="parameter">base</code></em> parameter has no effect unless the <em><code class="parameter">var</code></em> parameter is a string. </p> </p></blockquote> </div> <div class="refsect1 changelog" id="refsect1-function.intval-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.1.0</td> <td> Throws <strong><code>E_NOTICE</code></strong> and returns 1, when an object is passed to <em><code class="parameter">var</code></em>. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 seealso" id="refsect1-function.intval-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.boolval.html" class="function" rel="rdfs-seeAlso">boolval()</a> - Get the boolean value of a variable</span></li> <li class="member"> <span class="function"><a href="function.floatval.html" class="function" rel="rdfs-seeAlso">floatval()</a> - Get float value of a variable</span></li> <li class="member"> <span class="function"><a href="function.strval.html" class="function" rel="rdfs-seeAlso">strval()</a> - Get string value of a variable</span></li> <li class="member"> <span class="function"><a href="function.settype.html" class="function" rel="rdfs-seeAlso">settype()</a> - Set the type of a variable</span></li> <li class="member"> <span class="function"><a href="function.is-numeric.html" class="function" rel="rdfs-seeAlso">is_numeric()</a> - Finds whether a variable is a number or a numeric string</span></li> <li class="member"><a href="language.types.type-juggling.html" class="link">Type juggling</a></li> <li class="member"><a href="ref.bc.html" class="link">BCMath Arbitrary Precision Mathematics Functions</a></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.import-request-variables.html">import_request_variables</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.is-array.html">is_array</a></div> <div class="up"><a href="ref.var.html">Variable handling Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>