chr()
The chr() function is used to return a specific character based on the ASCII code. The syntax of the chr() function is as follows:
The PHP chr() function returns a single-byte string containing the character that corresponds to a given byte value (commonly an ASCII code). It is the inverse of ord(), which goes the other way — from a character to its numeric code.
This page covers the syntax of chr(), how it handles values outside the 0–255 range, and practical patterns for building strings from numeric codes.
Syntax
chr(int $codepoint): stringchr() takes one parameter, $codepoint, the numeric value of the byte to return, and returns a one-character (one-byte) string. The value is interpreted in the range 0–255. Values outside that range are wrapped using modulo 256: PHP repeatedly adds or subtracts 256 until the value falls between 0 and 255. So chr(321) is the same as chr(321 - 256), i.e. chr(65), and chr(-1) is the same as chr(255).
Basic example
The most common use is turning an ASCII code into its character. The code 65 is the letter A:
Output:
AWe pass the ASCII code to chr(), and it returns the matching character.
Generating a sequence of characters
Because letters have consecutive ASCII codes, you can loop over a numeric range to build a sequence. Uppercase A–Z runs from 65 to 90:
Output:
A B C D E F G H I J K L M N O P Q R S T U V W X Y ZEach ASCII code is passed to chr(), which returns the corresponding letter.
Values outside 0–255
chr() never errors on an out-of-range value — it wraps around with modulo 256 instead. This is worth knowing so you do not get surprising results when arithmetic pushes a value past the byte range:
<?php
echo chr(65) . "\n"; // A (in range)
echo chr(321) . "\n"; // A 321 - 256 = 65
echo chr(256 + 65); // A wraps back to 65
?>All three lines print A, because 321 and 256 + 65 both reduce to 65.
chr() vs. ord()
chr() and ord() are a complementary pair:
chr(int)→ returns the character for a byte value.ord(string)→ returns the byte value of the first character of a string.
<?php
$code = ord("A"); // 65
echo chr($code); // A
?>Running a value through ord() and then chr() (or vice versa) returns the original — they round-trip.
A note on multibyte text
chr() works on single bytes, so it is reliable only for ASCII (0–127) and other single-byte encodings. It does not produce a UTF-8 multibyte character: passing a Unicode code point above 255 will not give you the matching emoji or accented letter — it will wrap into the 0–255 range. For full Unicode code points, use mb_chr() (and mb_ord() for the reverse) from the mbstring extension.
Related functions
ord()— the inverse: character to its ASCII/byte value.str_split()— split a string into an array of characters.strtoupper()— convert a string to uppercase.- PHP Strings — overview of string handling in PHP.