<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.types.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'language.types.type-juggling.php',
    1 => 'Type Juggling',
    2 => 'Type Juggling',
  ),
  'up' => 
  array (
    0 => 'language.types.php',
    1 => 'Types',
  ),
  'prev' => 
  array (
    0 => 'language.types.declarations.php',
    1 => 'Type declarations',
  ),
  'next' => 
  array (
    0 => 'language.variables.php',
    1 => 'Variables',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/types/type-juggling.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.types.type-juggling" class="sect1">
 <h2 class="title">Type Juggling</h2>

 <p class="simpara">
  PHP does not require explicit type definition in variable declaration.
  In this case, the type of a variable is determined by the value it stores.
  That is to say, if a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> is assigned to variable
  <var class="varname">$var</var>, then <var class="varname">$var</var> is of type
  <span class="type"><a href="language.types.string.php" class="type string">string</a></span>. If afterwards an <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> value is assigned
  to <var class="varname">$var</var>, it will be of type <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>.
 </p>

 <p class="para">
  PHP may attempt to convert the type of a value to another automatically
  in certain contexts. The different contexts which exist are:
  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">Numeric</span>
   </li>
   <li class="listitem">
    <span class="simpara">String</span>
   </li>
   <li class="listitem">
    <span class="simpara">Logical</span>
   </li>
   <li class="listitem">
    <span class="simpara">Integral and string</span>
   </li>
   <li class="listitem">
    <span class="simpara">Comparative</span>
   </li>
   <li class="listitem">
    <span class="simpara">Function</span>
   </li>
  </ul>
 </p>

 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <span class="simpara">
   When a value needs to be interpreted as a different type, the value itself
   does <em>not</em> change types.
  </span>
 </p></blockquote>

 <p class="simpara">
  To force a variable to be evaluated as a certain type, see the section on
  <a href="language.types.type-juggling.php#language.types.typecasting" class="link">Type casting</a>. To change the
  type of a variable, see the <span class="function"><a href="function.settype.php" class="function">settype()</a></span> function.
 </p>

 <div class="sect2">
  <h3 class="title">Numeric contexts</h3>

  <p class="simpara">
   This is the context when using an
   <a href="language.operators.arithmetic.php" class="link">arithmetical operator</a>.
  </p>

  <p class="simpara">
   In this context if either operand is a <span class="type"><a href="language.types.float.php" class="type float">float</a></span> (or not
   interpretable as an <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>), both operands are interpreted as
   <span class="type"><a href="language.types.float.php" class="type float">float</a></span>s, and the result will be a <span class="type"><a href="language.types.float.php" class="type float">float</a></span>.
   Otherwise, the operands will be interpreted as <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>s,
   and the result will also be an <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>.
   As of PHP 8.0.0, if one of the operands cannot be interpreted a
   <span class="classname"><a href="class.typeerror.php" class="classname">TypeError</a></span> is thrown.
  </p>
 </div>

 <div class="sect2">
  <h3 class="title">String contexts</h3>

  <p class="simpara">
   This is the context when using <span class="function"><a href="function.echo.php" class="function">echo</a></span>,
   <span class="function"><a href="function.print.php" class="function">print</a></span>,
   <a href="language.types.string.php#language.types.string.parsing" class="link">string interpolation</a>,
   or the string
   <a href="language.operators.string.php" class="link">concatenation operator</a>.
  </p>

  <p class="simpara">
   In this context the value will be interpreted as <span class="type"><a href="language.types.string.php" class="type string">string</a></span>.
   If the value cannot be interpreted a <span class="classname"><a href="class.typeerror.php" class="classname">TypeError</a></span> is thrown.
   Prior to PHP 7.4.0, an <strong><code><a href="errorfunc.constants.php#constant.e-recoverable-error">E_RECOVERABLE_ERROR</a></code></strong> was raised.
  </p>
 </div>

 <div class="sect2">
  <h3 class="title">Logical contexts</h3>

  <p class="simpara">
   This is the context when using conditional statements, the
   <a href="language.operators.comparison.php#language.operators.comparison.ternary" class="link">ternary operator</a>,
   or a <a href="language.operators.logical.php" class="link">logical operator</a>.
  </p>

  <p class="simpara">
   In this context the value will be interpreted as <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span>.
  </p>
 </div>

 <div class="sect2">
  <h3 class="title">Integral and string contexts</h3>

  <p class="simpara">
   This is the context when using
   <a href="language.operators.bitwise.php" class="link">bitwise operators</a>.
  </p>

  <p class="simpara">
   In this context if all operands are of type <span class="type"><a href="language.types.string.php" class="type string">string</a></span> the result
   will also be a <span class="type"><a href="language.types.string.php" class="type string">string</a></span>.
   Otherwise, the operands will be interpreted as <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>s,
   and the result will also be an <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>.
   As of PHP 8.0.0, if one of the operands cannot be interpreted a
   <span class="classname"><a href="class.typeerror.php" class="classname">TypeError</a></span> is thrown.
  </p>
 </div>

 <div class="sect2">
  <h3 class="title">Comparative contexts</h3>

  <p class="simpara">
   This is the context when using a
   <a href="language.operators.comparison.php" class="link">comparison operator</a>.
  </p>

  <p class="simpara">
   The type conversions which occur in this context are explained in the
   Comparison with Various Types
   <a href="language.operators.comparison.php#language.operators.comparison.types" class="link">table</a>.
  </p>
 </div>

 <div class="sect2" id="language.types.type-juggling.function">
  <h3 class="title">Function contexts</h3>

  <p class="simpara">
   This is the context when a value is passed to a typed parameter, property,
   or returned from a function which declares a return type.
  </p>

  <p class="para">
   In this context the value must be a value of the type.
   Two exceptions exist, the first one is: if the value is of type
   <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> and the declared type is <span class="type"><a href="language.types.float.php" class="type float">float</a></span>, then the
   integer is converted to a floating point number.
   The second one is: if the declared type is a <em>scalar</em>
   
   type, the value is convertable to a scalar type,
   and the coercive typing mode is active
   (the default), the value may be converted to an accepted scalar value.
   See below for a description of this behaviour.
  </p>

  <div class="warning"><strong class="warning">Warning</strong>
   <p class="simpara">
    <a href="functions.internal.php" class="link">Internal functions</a>
    automatically coerce <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> to scalar types,
    this behaviour is <em>DEPRECATED</em> as of PHP 8.1.0.
   </p>
  </div>

  <div class="sect3" id="language.types.type-juggling.function.simple">
   <h4 class="title">Coercive typing with simple type declarations</h4>
   <ul class="itemizedlist">
    <li class="listitem">
     <span class="simpara">
      <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> type declaration: value is interpreted as <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span>.
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> type declaration: value is interpreted as <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>
      if the conversion is well-defined. For example the string is
      <a href="language.types.numeric-strings.php" class="link">numeric</a>.
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <span class="type"><a href="language.types.float.php" class="type float">float</a></span> type declaration: value is interpreted as <span class="type"><a href="language.types.float.php" class="type float">float</a></span>
      if the conversion is well-defined. For example the string is
      <a href="language.types.numeric-strings.php" class="link">numeric</a>.
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <span class="type"><a href="language.types.string.php" class="type string">string</a></span> type declaration: value is interpreted as <span class="type"><a href="language.types.string.php" class="type string">string</a></span>.
     </span>
    </li>
   </ul>
  </div>

  <div class="sect3" id="language.types.type-juggling.function.union">
   <h4 class="title">Coercive typing with union types</h4>
   <p class="para">
    When <code class="literal">strict_types</code> is not enabled, scalar type declarations
    are subject to limited implicit type coercions.
    If the exact type of the value is not part of the union, then the target type
    is chosen in the following order of preference:

    <ol type="1">
     <li class="listitem">
      <span class="simpara">
       <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       <span class="type"><a href="language.types.float.php" class="type float">float</a></span>
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       <span class="type"><a href="language.types.string.php" class="type string">string</a></span>
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span>
      </span>
     </li>
    </ol>

    If the type exists in the union and the value can be coerced to the
    type under PHP&#039;s existing type-checking semantics, then the type is chosen.
    Otherwise, the next type is tried.
   </p>

   <div class="caution"><strong class="caution">Caution</strong>
    <p class="para">
     As an exception, if the value is a string and both int and float are part
     of the union, the preferred type is determined by the existing
     <a href="language.types.numeric-strings.php" class="link">numeric string</a>
     semantics.
     For example, for <code class="literal">&quot;42&quot;</code> <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> is chosen,
     while for <code class="literal">&quot;42.0&quot;</code> <span class="type"><a href="language.types.float.php" class="type float">float</a></span> is chosen.
    </p>
   </div>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     Types that are not part of the above preference list are not eligible
     targets for implicit coercion. In particular no implicit coercions to
     the <span class="type"><a href="language.types.null.php" class="type null">null</a></span>, <span class="type"><a href="language.types.singleton.php" class="type false">false</a></span>, and <span class="type"><a href="language.types.singleton.php" class="type true">true</a></span>
     types occur.
    </p>
   </p></blockquote>

   <div class="example" id="example-1">
    <p><strong>Example #1 Example of types being coerced into a type part of the union</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// int|string<br /></span><span style="color: #0000BB">42    </span><span style="color: #007700">--&gt; </span><span style="color: #0000BB">42          </span><span style="color: #FF8000">// exact type<br /></span><span style="color: #DD0000">"42"  </span><span style="color: #007700">--&gt; </span><span style="color: #DD0000">"42"        </span><span style="color: #FF8000">// exact type<br /></span><span style="color: #007700">new </span><span style="color: #0000BB">ObjectWithToString </span><span style="color: #007700">--&gt; </span><span style="color: #DD0000">"Result of __toString()"<br />                      </span><span style="color: #FF8000">// object never compatible with int, fall back to string<br /></span><span style="color: #0000BB">42.0  </span><span style="color: #007700">--&gt; </span><span style="color: #0000BB">42          </span><span style="color: #FF8000">// float compatible with int<br /></span><span style="color: #0000BB">42.1  </span><span style="color: #007700">--&gt; </span><span style="color: #0000BB">42          </span><span style="color: #FF8000">// float compatible with int<br /></span><span style="color: #0000BB">1e100 </span><span style="color: #007700">--&gt; </span><span style="color: #DD0000">"1.0E+100"  </span><span style="color: #FF8000">// float too large for int type, fall back to string<br /></span><span style="color: #0000BB">INF   </span><span style="color: #007700">--&gt; </span><span style="color: #DD0000">"INF"       </span><span style="color: #FF8000">// float too large for int type, fall back to string<br /></span><span style="color: #0000BB">true  </span><span style="color: #007700">--&gt; </span><span style="color: #0000BB">1           </span><span style="color: #FF8000">// bool compatible with int<br /></span><span style="color: #007700">[]    --&gt; </span><span style="color: #0000BB">TypeError   </span><span style="color: #FF8000">// array not compatible with int or string<br /><br />// int|float|bool<br /></span><span style="color: #DD0000">"45"    </span><span style="color: #007700">--&gt; </span><span style="color: #0000BB">45        </span><span style="color: #FF8000">// int numeric string<br /></span><span style="color: #DD0000">"45.0"  </span><span style="color: #007700">--&gt; </span><span style="color: #0000BB">45.0      </span><span style="color: #FF8000">// float numeric string<br /><br /></span><span style="color: #DD0000">"45X"   </span><span style="color: #007700">--&gt; </span><span style="color: #0000BB">true      </span><span style="color: #FF8000">// not numeric string, fall back to bool<br /></span><span style="color: #DD0000">""      </span><span style="color: #007700">--&gt; </span><span style="color: #0000BB">false     </span><span style="color: #FF8000">// not numeric string, fall back to bool<br /></span><span style="color: #DD0000">"X"     </span><span style="color: #007700">--&gt; </span><span style="color: #0000BB">true      </span><span style="color: #FF8000">// not numeric string, fall back to bool<br /></span><span style="color: #007700">[]      --&gt; </span><span style="color: #0000BB">TypeError </span><span style="color: #FF8000">// array not compatible with int, float or bool<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </div>

 </div>

 <div class="sect2" id="language.types.typecasting">
  <h3 class="title">Type Casting</h3>

  <p class="simpara">
   Type casting converts the value to a chosen type by writing the type within
   parentheses before the value to convert.
  </p>

  <div class="example" id="example-2">
   <p><strong>Example #2 Type Casting</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: #0000BB">10</span><span style="color: #007700">;          </span><span style="color: #FF8000">// $foo is an integer<br /></span><span style="color: #0000BB">$bar </span><span style="color: #007700">= (bool) </span><span style="color: #0000BB">$foo</span><span style="color: #007700">; </span><span style="color: #FF8000">// $bar is a boolean<br /><br /></span><span style="color: #0000BB">var_dump</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 class="simpara">
   The casts allowed are:
  </p>

  <ul class="simplelist">
   <li><code class="literal">(int)</code> - cast to <span class="type"><a href="language.types.integer.php" class="type int">int</a></span></li>
   <li><code class="literal">(bool)</code> - cast to <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></li>
   <li><code class="literal">(float)</code> - cast to <span class="type"><a href="language.types.float.php" class="type float">float</a></span></li>
   <li><code class="literal">(string)</code> - cast to <span class="type"><a href="language.types.string.php" class="type string">string</a></span></li>
   <li><code class="literal">(array)</code> - cast to <span class="type"><a href="language.types.array.php" class="type array">array</a></span></li>
   <li><code class="literal">(object)</code> - cast to <span class="type"><a href="language.types.object.php" class="type object">object</a></span></li>
   <li><code class="literal">(unset)</code> - cast to <span class="type"><a href="language.types.null.php" class="type NULL">NULL</a></span></li>
  </ul>

  <div class="warning"><strong class="warning">Warning</strong>
   <p class="para">
    <code class="literal">(integer)</code> is an alias of the <code class="literal">(int)</code> cast.
    <code class="literal">(boolean)</code> is an alias of the <code class="literal">(bool)</code> cast.
    <code class="literal">(binary)</code> is an alias of the <code class="literal">(string)</code> cast.
    <code class="literal">(double)</code> and <code class="literal">(real)</code> are aliases of
    the <code class="literal">(float)</code> cast.
    These casts do not use the canonical type name and are deprecated as of PHP 8.5.0.
   </p>
  </div>

  <div class="warning"><strong class="warning">Warning</strong>
   <p class="simpara">
    The <code class="literal">(real)</code> cast alias has been deprecated as of PHP 7.4.0
    and removed as of PHP 8.0.0.
   </p>
  </div>

  <div class="warning"><strong class="warning">Warning</strong>
   <p class="simpara">
    The <code class="literal">(unset)</code> cast has been deprecated as of PHP 7.2.0.
    Note that the <code class="literal">(unset)</code> cast is the same as assigning the
    value <span class="type"><a href="language.types.null.php" class="type NULL">NULL</a></span> to the variable or call.
    The <code class="literal">(unset)</code> cast is removed as of PHP 8.0.0.
   </p>
  </div>

  <div class="caution"><strong class="caution">Caution</strong>
   <p class="simpara">
    The <code class="literal">(binary)</code> cast and <code class="literal">b</code> prefix exists
    for forward support. Currently <code class="literal">(binary)</code> and
    <code class="literal">(string)</code> are identical, however this may change and
    should not be relied upon.
   </p>
  </div>

  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Whitespaces are ignored within the parentheses of a cast.
    Therefore, the following two casts are equivalent:
    <div class="informalexample">
     <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">= (int) </span><span style="color: #0000BB">$bar</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= ( int ) </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>
  </p></blockquote>

  <div class="informalexample">
   <p class="simpara">
    Casting literal <span class="type"><a href="language.types.string.php" class="type string">string</a></span>s and variables to binary
    <span class="type"><a href="language.types.string.php" class="type string">string</a></span>s:
   </p>

   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$binary </span><span style="color: #007700">= (binary) </span><span style="color: #0000BB">$string</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$binary </span><span style="color: #007700">= </span><span style="color: #DD0000">b"binary string"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

  
  <p class="simpara">
   Instead of casting a variable to a <span class="type"><a href="language.types.string.php" class="type string">string</a></span>, it is also possible
   to enclose the variable in double quotes.
  </p>

  <div class="example" id="example-3">
   <p><strong>Example #3 Different Casting Mechanisms</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: #0000BB">10</span><span style="color: #007700">;            </span><span style="color: #FF8000">// $foo is an integer<br /></span><span style="color: #0000BB">$str </span><span style="color: #007700">= </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$foo</span><span style="color: #DD0000">"</span><span style="color: #007700">;        </span><span style="color: #FF8000">// $str is a string<br /></span><span style="color: #0000BB">$fst </span><span style="color: #007700">= (string) </span><span style="color: #0000BB">$foo</span><span style="color: #007700">; </span><span style="color: #FF8000">// $fst is also a string<br /><br />// This prints out that "they are the same"<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">$fst </span><span style="color: #007700">=== </span><span style="color: #0000BB">$str</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">"they are the same"</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

  <p class="para">
   It may not be obvious exactly what will happen when casting between certain
   types. For more information, see these sections:
   <ul class="simplelist">
    <li><a href="language.types.boolean.php#language.types.boolean.casting" class="link">Converting to boolean</a></li>
    <li><a href="language.types.integer.php#language.types.integer.casting" class="link">Converting to integer</a></li>
    <li><a href="language.types.float.php#language.types.float.casting" class="link">Converting to float</a></li>
    <li><a href="language.types.string.php#language.types.string.casting" class="link">Converting to string</a></li>
    <li><a href="language.types.array.php#language.types.array.casting" class="link">Converting to array</a></li>
    <li><a href="language.types.object.php#language.types.object.casting" class="link">Converting to object</a></li>
    <li><a href="language.types.resource.php#language.types.resource.casting" class="link">Converting to resource</a></li>
    <li><a href="language.types.null.php#language.types.null.casting" class="link">Converting to NULL</a></li>
    <li><a href="types.comparisons.php" class="link">The type comparison tables</a></li>
   </ul>
  </p>

  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    Because PHP supports indexing into <span class="type"><a href="language.types.string.php" class="type string">string</a></span>s via offsets
    using the same syntax as <span class="type"><a href="language.types.array.php" class="type array">array</a></span> indexing, the following example
    holds true for all PHP versions:
   </span>

   <div class="example" id="example-4">
    <p><strong>Example #4 Using Array Offset with a String</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$a    </span><span style="color: #007700">= </span><span style="color: #DD0000">'car'</span><span style="color: #007700">; </span><span style="color: #FF8000">// $a is a string<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">] = </span><span style="color: #DD0000">'b'</span><span style="color: #007700">;   </span><span style="color: #FF8000">// $a is still a string<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$a</span><span style="color: #007700">;       </span><span style="color: #FF8000">// bar<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <span class="simpara">
    See the section titled <a href="language.types.string.php#language.types.string.substr" class="link">String
    access by character</a> for more information.
   </span>
  </p></blockquote>
 </div>

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