chr
(PHP 4, PHP 5, PHP 7)
chr — Generate a single-byte string from a number
Description
$bytevalue
) : string
Returns a one-character string containing the character specified
by interpreting bytevalue
as an unsigned integer.
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.
This function complements ord().
Parameters
-
bytevalue
-
An integer between 0 and 255.
Values outside the valid range (0..255) will be bitwise and'ed with 255, which is equivalent to the following algorithm:
while ($bytevalue < 0) {
$bytevalue += 256;
}
$bytevalue %= 256;
Examples
Example #1 chr() example
<?php
// Assumes the string will be used as ASCII or an ASCII-compatible encoding
$str = "The string ends in escape: ";
$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);
?>
Example #2 Overflow behavior
<?php
echo chr(-159), chr(833), PHP_EOL;
?>
The above example will output:
aA
Example #3 Building a UTF-8 string from individual bytes
<?php
declare(encoding='UTF-8');
$str = chr(240) . chr(159) . chr(144) . chr(152);
echo $str;
?>
The above example will output:
🐘
See Also
- sprintf() - Return a formatted string with a format string of %c
- ord()
- An » ASCII-table
- mb_chr()
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-function.chr.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.