<!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>Generate a single-byte string from a number</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.chop.html">chop</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.chunk-split.html">chunk_split</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.chr" class="refentry"> <div class="refnamediv"> <h1 class="refname">chr</h1> <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">chr</span> — <span class="dc-title">Generate a single-byte string from a number</span></p> </div> <div class="refsect1 description" id="refsect1-function.chr-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>chr</strong></span> ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$bytevalue</code></span> )</div> <p class="para rdfs-comment"> Returns a one-character string containing the character specified by interpreting <code class="parameter">bytevalue</code> as an unsigned integer. </p> <p class="para"> This can be used to create a one-character string in a single-byte encoding such as ASCII, ISO-8859, or Windows 1252, by passing the position of a desired character in the encoding's mapping table. However, note that this function is not aware of any string encoding, and in particular cannot be passed a Unicode code point value to generate a string in a multibyte encoding like UTF-8 or UTF-16. </p> <p class="para"> This function complements <span class="function"><a href="function.ord.html" class="function">ord()</a></span>. </p> </div> <div class="refsect1 parameters" id="refsect1-function.chr-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">bytevalue</code></dt> <dd> <p class="para"> An integer between 0 and 255. </p> <p class="para"> Values outside the valid range (0..255) will be bitwise and'ed with 255, which is equivalent to the following algorithm: <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> while ($bytevalue < 0) {<br /> $bytevalue += 256;<br />}<br />$bytevalue %= 256;</span> </code></div> </div> </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.chr-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> A single-character string containing the specified byte. </p> </div> <div class="refsect1 examples" id="refsect1-function.chr-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-6020"> <p><strong>Example #1 <span class="function"><strong>chr()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Assumes the string will be used as ASCII or an ASCII-compatible encoding<br /><br /></span><span style="color: #0000BB">$str </span><span style="color: #007700">= </span><span style="color: #DD0000">"The string ends in escape: "</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$str </span><span style="color: #007700">.= </span><span style="color: #0000BB">chr</span><span style="color: #007700">(</span><span style="color: #0000BB">27</span><span style="color: #007700">); </span><span style="color: #FF8000">/* add an escape character at the end of $str */<br /><br />/* Often this is more useful */<br /><br /></span><span style="color: #0000BB">$str </span><span style="color: #007700">= </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"The string ends in escape: %c"</span><span style="color: #007700">, </span><span style="color: #0000BB">27</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <div class="example" id="example-6021"> <p><strong>Example #2 Overflow behavior</strong></p> <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">chr</span><span style="color: #007700">(-</span><span style="color: #0000BB">159</span><span style="color: #007700">), </span><span style="color: #0000BB">chr</span><span style="color: #007700">(</span><span style="color: #0000BB">833</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> aA </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-6022"> <p><strong>Example #3 Building a UTF-8 string from individual bytes</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">declare(</span><span style="color: #0000BB">encoding</span><span style="color: #007700">=</span><span style="color: #DD0000">'UTF-8'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$str </span><span style="color: #007700">= </span><span style="color: #0000BB">chr</span><span style="color: #007700">(</span><span style="color: #0000BB">240</span><span style="color: #007700">) . </span><span style="color: #0000BB">chr</span><span style="color: #007700">(</span><span style="color: #0000BB">159</span><span style="color: #007700">) . </span><span style="color: #0000BB">chr</span><span style="color: #007700">(</span><span style="color: #0000BB">144</span><span style="color: #007700">) . </span><span style="color: #0000BB">chr</span><span style="color: #007700">(</span><span style="color: #0000BB">152</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$str</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"><br /> 🐘<br /> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.chr-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><span class="function"><a href="function.sprintf.html" class="function" rel="rdfs-seeAlso">sprintf()</a> - Return a formatted string</span> with a format string of <em>%c</em></li> <li class="member"><span class="function"><a href="function.ord.html" class="function">ord()</a></span></li> <li class="member">An <a href="http://www.asciitable.com" class="link external">» ASCII-table</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.chop.html">chop</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.chunk-split.html">chunk_split</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>