<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.strings.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.crc32.php',
    1 => 'crc32',
    2 => 'Calculates the crc32 polynomial of a string',
  ),
  'up' => 
  array (
    0 => 'ref.strings.php',
    1 => 'String Functions',
  ),
  'prev' => 
  array (
    0 => 'function.count-chars.php',
    1 => 'count_chars',
  ),
  'next' => 
  array (
    0 => 'function.crypt.php',
    1 => 'crypt',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/strings/functions/crc32.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.crc32" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">crc32</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.1, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">crc32</span> &mdash; <span class="dc-title">Calculates the crc32 polynomial of a string</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.crc32-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>crc32</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$string</code></span>): <span class="type"><a href="language.types.integer.php" class="type int">int</a></span></div>

  <p class="para rdfs-comment">
   Generates the cyclic redundancy checksum polynomial of 32-bit
   lengths of the <code class="parameter">string</code>. This is usually used
   to validate the integrity of data being transmitted.
  </p>
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="simpara">
    Because PHP&#039;s integer type is signed many crc32 checksums will
    result in negative integers on 32bit platforms. On 64bit installations
    all <span class="function"><strong>crc32()</strong></span> results will be positive integers though.
   </p>
   <p class="simpara">
    So you need to use the &quot;%u&quot; formatter of <span class="function"><a href="function.sprintf.php" class="function">sprintf()</a></span> or 
    <span class="function"><a href="function.printf.php" class="function">printf()</a></span> to get the string representation of the 
    unsigned <span class="function"><strong>crc32()</strong></span> checksum in decimal format.
   </p>
   <p class="simpara">
    For a hexadecimal representation of the checksum you can either use the 
    &quot;%x&quot; formatter of <span class="function"><a href="function.sprintf.php" class="function">sprintf()</a></span> or <span class="function"><a href="function.printf.php" class="function">printf()</a></span> 
    or the <span class="function"><a href="function.dechex.php" class="function">dechex()</a></span> conversion functions, both of these
    also take care of converting the <span class="function"><strong>crc32()</strong></span> result to
    an unsigned integer.
   </p>
   <p class="simpara">
    Having 64bit installations also return negative integers for higher
    result values was considered but would break the hexadecimal conversion
    as negatives would get an extra 0xFFFFFFFF######## offset then. As 
    hexadecimal representation seems to be the most common use case we
    decided to not break this even if it breaks direct decimal comparisons
    in about 50% of the cases when moving from 32 to 64bits.
   </p>
   <p class="simpara">
    In retrospect having the function return an integer maybe wasn&#039;t the 
    best idea and returning a hex string representation right away (as 
    e.g. <span class="function"><a href="function.md5.php" class="function">md5()</a></span> does) might have been a better plan to
    begin with.
   </p>
   <p class="simpara">
    For a more portable solution you may also consider the generic 
    <span class="function"><a href="function.hash.php" class="function">hash()</a></span>. <code class="code">hash(&quot;crc32b&quot;, $str)</code> will 
    return the same string as <code class="code">str_pad(dechex(crc32($str)), 8, &#039;0&#039;, STR_PAD_LEFT)</code>.
   </p>
  </div>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.crc32-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">string</code></dt>
     <dd>
      <p class="para">
       The data.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.crc32-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns the crc32 checksum of <code class="parameter">string</code> as an integer.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.crc32-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 Displaying a crc32 checksum</strong></p>
    <div class="example-contents"><p>
     This example shows how to print a converted checksum with the
     <span class="function"><a href="function.printf.php" class="function">printf()</a></span> function:
    </p></div>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$checksum </span><span style="color: #007700">= </span><span style="color: #0000BB">crc32</span><span style="color: #007700">(</span><span style="color: #DD0000">"The quick brown fox jumped over the lazy dog."</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%u\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$checksum</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

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


 <div class="refsect1 seealso" id="refsect1-function.crc32-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.hash.php" class="function" rel="rdfs-seeAlso">hash()</a> - Generate a hash value (message digest)</span></li>
    <li><span class="function"><a href="function.md5.php" class="function" rel="rdfs-seeAlso">md5()</a> - Calculate the md5 hash of a string</span></li>
    <li><span class="function"><a href="function.sha1.php" class="function" rel="rdfs-seeAlso">sha1()</a> - Calculate the sha1 hash of a string</span></li>
   </ul>
  </p>
 </div>


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