2010-03-28 22:10:10 +00:00
<?xml version="1.0" encoding="utf-8"?>
2009-07-11 09:08:43 +00:00
<!-- $Revision$ -->
2007-06-20 22:25:43 +00:00
<refentry xmlns:xlink= "http://www.w3.org/1999/xlink" xmlns= "http://docbook.org/ns/docbook" xml:id= "function.chr" >
2007-06-11 00:50:00 +00:00
<refnamediv >
<refname > chr</refname>
2018-06-28 10:36:19 +00:00
<refpurpose > Generate a single-byte string from a number</refpurpose>
2007-06-11 00:50:00 +00:00
</refnamediv>
2007-06-11 00:50:48 +00:00
<refsect1 role= "description" >
&reftitle.description;
2007-06-11 00:50:00 +00:00
<methodsynopsis >
<type > string</type> <methodname > chr</methodname>
2020-12-05 14:30:42 +00:00
<methodparam > <type > int</type> <parameter > codepoint</parameter> </methodparam>
2007-06-11 00:50:00 +00:00
</methodsynopsis>
<para >
Returns a one-character string containing the character specified
2020-12-05 14:30:42 +00:00
by interpreting <parameter > codepoint</parameter> as an unsigned integer.
2018-06-28 10:36:19 +00:00
</para>
<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.
2007-06-11 00:50:48 +00:00
</para>
<para >
This function complements <function > ord</function> .
</para>
</refsect1>
<refsect1 role= "parameters" >
&reftitle.parameters;
<para >
<variablelist >
<varlistentry >
2020-12-05 14:30:42 +00:00
<term > <parameter > codepoint</parameter> </term>
2007-06-11 00:50:48 +00:00
<listitem >
<para >
2018-06-28 10:36:19 +00:00
An integer between 0 and 255.
2016-07-03 17:23:12 +00:00
</para>
<para >
Values outside the valid range (0..255) will be bitwise and'ed with 255,
2016-08-05 13:14:41 +00:00
which is equivalent to the following algorithm:
2016-07-03 17:23:12 +00:00
<programlisting role= "php" >
< ![CDATA[
2018-06-28 10:36:19 +00:00
while ($bytevalue < 0 ) {
$bytevalue += 256;
2016-07-03 17:23:12 +00:00
}
2018-06-28 10:36:19 +00:00
$bytevalue %= 256;
2016-07-03 17:23:12 +00:00
]]>
</programlisting>
2007-06-11 00:50:48 +00:00
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role= "returnvalues" >
&reftitle.returnvalues;
<para >
2018-06-28 10:36:19 +00:00
A single-character string containing the specified byte.
2007-06-11 00:50:48 +00:00
</para>
</refsect1>
2020-11-04 13:23:13 +00:00
<refsect1 role= "changelog" >
&reftitle.changelog;
<informaltable >
<tgroup cols= "2" >
<thead >
<row >
<entry > &Version; </entry>
<entry > &Description; </entry>
</row>
</thead>
<tbody >
<row >
<entry > 7.4.0</entry>
<entry >
2020-12-05 14:30:42 +00:00
The function no longer silently accepts unsupported <parameter > codepoint</parameter> s,
2020-11-04 13:23:13 +00:00
and casts these to <literal > 0</literal> .
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</refsect1>
2007-06-11 00:50:48 +00:00
<refsect1 role= "examples" >
&reftitle.examples;
<para >
2007-06-11 00:50:00 +00:00
<example >
<title > <function > chr</function> example</title>
<programlisting role= "php" >
2002-04-15 00:12:54 +00:00
< ![CDATA[
2003-05-30 16:47:59 +00:00
< ?php
2018-06-28 10:36:19 +00:00
// Assumes the string will be used as ASCII or an ASCII-compatible encoding
2003-10-19 21:13:22 +00:00
$str = "The string ends in escape: ";
2002-04-15 00:12:54 +00:00
$str .= chr(27); /* add an escape character at the end of $str */
/* Often this is more useful */
$str = sprintf("The string ends in escape: %c", 27);
2003-05-30 16:47:59 +00:00
?>
2002-04-15 00:12:54 +00:00
]]>
2007-06-11 00:50:00 +00:00
</programlisting>
</example>
2016-07-01 20:07:17 +00:00
<example >
<title > Overflow behavior</title>
<programlisting role= "php" >
< ![CDATA[
< ?php
echo chr(-159), chr(833), PHP_EOL;
?>
]]>
</programlisting>
&example.outputs;
<screen >
< ![CDATA[
aA
]]>
</screen>
</example>
2007-06-11 00:50:00 +00:00
</para>
2018-06-28 10:36:19 +00:00
<para >
<example >
<title > Building a UTF-8 string from individual bytes</title>
<programlisting role= "php" >
< ![CDATA[
< ?php
$str = chr(240) . chr(159) . chr(144) . chr(152);
echo $str;
?>
]]>
</programlisting>
&example.outputs;
<screen >
🐘
</screen>
</example>
</para>
2007-06-11 00:50:48 +00:00
</refsect1>
<refsect1 role= "seealso" >
&reftitle.seealso;
2007-06-11 00:50:00 +00:00
<para >
2007-06-11 00:50:48 +00:00
<simplelist >
<member > <function > sprintf</function> with a format string of <literal > %c</literal> </member>
2008-01-22 18:30:03 +00:00
<member > <function > ord</function> </member>
2007-06-20 22:25:43 +00:00
<member > An <link xlink:href= "&url.asciitable;" > ASCII-table</link> </member>
2018-12-28 14:58:12 +00:00
<member > <function > mb_chr</function> </member>
2007-06-11 00:50:48 +00:00
</simplelist>
2007-06-11 00:50:00 +00:00
</para>
</refsect1>
2007-06-11 00:50:48 +00:00
2007-06-11 00:50:00 +00:00
</refentry>
2002-04-15 00:12:54 +00:00
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
2009-09-25 07:04:39 +00:00
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
2002-04-15 00:12:54 +00:00
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->