<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.var.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.settype.php',
    1 => 'settype',
    2 => 'Set the type of a variable',
  ),
  'up' => 
  array (
    0 => 'ref.var.php',
    1 => 'Variable handling Functions',
  ),
  'prev' => 
  array (
    0 => 'function.serialize.php',
    1 => 'serialize',
  ),
  'next' => 
  array (
    0 => 'function.strval.php',
    1 => 'strval',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/var/functions/settype.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.settype" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">settype</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">settype</span> &mdash; <span class="dc-title">Set the type of a variable</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.settype-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>settype</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter reference">&$var</code></span>, <span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$type</code></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

  <p class="para rdfs-comment">
   Set the type of variable <code class="parameter">var</code> to
   <code class="parameter">type</code>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.settype-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">var</code></dt>
     <dd>
      <p class="para">
       The variable being converted.
      </p>
     </dd>
    
    
     <dt><code class="parameter">type</code></dt>
     <dd>
      <p class="para">
       Possibles values of <code class="parameter">type</code> are:
       <ul class="itemizedlist">
        <li class="listitem">
         <span class="simpara">
          &quot;boolean&quot; or &quot;bool&quot;
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          &quot;integer&quot; or &quot;int&quot;
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          &quot;float&quot; or &quot;double&quot;
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          &quot;string&quot;
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          &quot;array&quot;
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          &quot;object&quot;
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          &quot;null&quot;
         </span>
        </li>
       </ul>
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.settype-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> on success or <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> on failure.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.settype-errors">
  <h3 class="title">Errors/Exceptions</h3>
  <p class="para">
   Throws a <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span> if the value of
   <code class="parameter">type</code> is not a valid type, as of PHP 8.0.0.
   Prior to PHP 8.0.0, a <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> was emitted
   and <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> was returned.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.settype-changelog">
  <h3 class="title">Changelog</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       Now throws a <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span> when an invalid
       type is passed to <code class="parameter">type</code>. Previously, a
       <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> was emitted and the function returned
       <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.settype-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>settype()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$foo </span><span style="color: #007700">= </span><span style="color: #DD0000">"5bar"</span><span style="color: #007700">; </span><span style="color: #FF8000">// string<br /></span><span style="color: #0000BB">$bar </span><span style="color: #007700">= </span><span style="color: #0000BB">true</span><span style="color: #007700">;   </span><span style="color: #FF8000">// boolean<br /><br /></span><span style="color: #0000BB">settype</span><span style="color: #007700">(</span><span style="color: #0000BB">$foo</span><span style="color: #007700">, </span><span style="color: #DD0000">"integer"</span><span style="color: #007700">); </span><span style="color: #FF8000">// $foo is now 5   (integer)<br /></span><span style="color: #0000BB">settype</span><span style="color: #007700">(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">, </span><span style="color: #DD0000">"string"</span><span style="color: #007700">);  </span><span style="color: #FF8000">// $bar is now "1" (string)<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$foo</span><span style="color: #007700">, </span><span style="color: #0000BB">$bar</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.settype-notes">
  <h3 class="title">Notes</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Maximum value for &quot;int&quot; is <strong><code><a href="reserved.constants.php#constant.php-int-max">PHP_INT_MAX</a></code></strong>.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.settype-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.gettype.php" class="function" rel="rdfs-seeAlso">gettype()</a> - Get the type of a variable</span></li>
    <li><a href="language.types.type-juggling.php#language.types.typecasting" class="link">type-casting</a></li>
    <li><a href="language.types.type-juggling.php" class="link">type-juggling</a></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>