The chr() function converts an integer code point into a single-character string. It is the go-to tool when you know the numeric ASCII value and need the corresponding letter, digit, or symbol—especially in alphabet and pattern programs.
01
Code to Char
Integer → one character.
02
ASCII Values
65 = A, 97 = a, etc.
03
ord() Inverse
Round-trip conversion.
04
One Parameter
Pass the code point.
05
A–Z Loops
Build letters in patterns.
06
Special Chars
Newline, tab, and more.
Fundamentals
Definition and Usage
In PHP, chr() takes an integer and returns a string containing exactly one character whose code point matches that number. For example, chr(65) returns "A" because 65 is the ASCII code for uppercase A. It is the opposite of ord(), which converts a character back to its numeric code.
💡
Beginner Tip
Remember: chr() = character from number. ord() = number from character. In alphabet pattern programs, looping from 65 to 90 and calling chr($i) generates A through Z automatically.
Foundation
📝 Syntax
The chr() function accepts one integer parameter:
PHP
string chr(int $codepoint)
Parameters
$codepoint — an integer representing the desired character code. Common ASCII range: 0–127. Extended bytes: 128–255. Unicode code points are supported in PHP 7.1+.
Return Value
Returns a string containing a single character. If the code point is invalid, PHP 8.0+ throws a ValueError.
Cheat Sheet
⚡ Quick Reference
Code
chr() Result
Meaning
65
"A"
Uppercase A
97
"a"
Lowercase a
48
"0"
Digit zero
10
"\n"
Newline
32
" "
Space
Basic
chr(65) // "A"
Code to character
Inverse
ord("A") // 65
Character to code
Loop
chr(64 + $i)
Generate A, B, C…
Special
chr(10) // newline
Control characters
Hands-On
Examples Gallery
Run these examples in any PHP 7+ environment. Each one demonstrates a common pattern beginners encounter with chr().
📚 Getting Started
Convert a single ASCII code into its character.
Example 1 — ASCII Code to Character
The classic beginner example: turn the number 65 into the letter A.
Line 1
Indented line
ord(newline) = 10
ord(tab) = 9
How It Works
Codes below 32 are control characters. In everyday code you usually write "\n" and "\t" directly, but chr() shows how those escape sequences map to numeric codes.
Applications
🚀 Common Use Cases
Alphabet and star patterns — generate A–Z dynamically inside nested loops.
Building strings from codes — construct characters when you only have numeric data.
Protocol and binary work — produce specific byte values for low-level formats.
Encoding exercises — pair with ord() to teach ASCII fundamentals.
Legacy code reading — understand programs that build text from integer tables.
🧠 How chr() Works
1
PHP reads the integer
You pass a code point—typically an ASCII value like 65 or 97.
Input
2
Code point is mapped to a byte
PHP looks up the character that corresponds to that numeric code in the current encoding.
Transform
3
A one-character string is returned
The result is always a string, even though it contains just one character.
Output
=
📝
Ready-to-use character
Concatenate, echo, or store in arrays for pattern building.
Important
📝 Notes
Standard ASCII covers codes 0–127. Extended ASCII uses 128–255.
ord() is the inverse: chr(ord($c)) === $c for single-byte characters.
For multi-byte UTF-8 text, prefer mb_chr() from the mbstring extension.
PHP 8.0+ throws ValueError for code points outside 0–1114111 (0x10FFFF).
In most tutorials, chr(64 + $i) where $i is 1–26 produces A–Z.
Wrap Up
Conclusion
The chr() function bridges numbers and characters in PHP. Whether you are printing a single letter from its ASCII code, generating an entire alphabet in a loop, or working with control characters, it turns integers into usable one-character strings.
Pair it with ord() for two-way conversion, and remember that modern Unicode text may need mb_chr() instead when working beyond single-byte characters.
Use chr() in loops to generate A–Z for pattern programs
Pair with ord() to verify conversions
Use named escapes like "\n" when readability matters
Validate code points when they come from user input
Use mb_chr() for Unicode beyond ASCII
❌ Don’t
Assume ord() works on multi-byte UTF-8 characters the same way
Pass out-of-range values without handling ValueError in PHP 8+
Confuse ASCII codes with string digits (chr(53) is "5", not "53")
Hard-code magic numbers without comments (prefer 65 /* A */ or constants)
Use chr() when a plain string literal is clearer
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about chr()
Use these points whenever you convert codes to characters in PHP.
5
Core concepts
📝01
Code → Char
Integer to string.
Purpose
🔄02
ord() Inverse
Char to code back.
Companion
🛠03
65 = A
Core ASCII fact.
Syntax
🎯04
Pattern Loops
65–90 for A–Z.
Use case
⚙05
mb_chr()
For Unicode text.
Modern PHP
❓ Frequently Asked Questions
chr() converts an integer code point into a single-character string. For example, chr(65) returns "A" because 65 is the ASCII code for uppercase A.
string chr(int $codepoint). You pass an integer (typically an ASCII or Unicode code point) and receive a one-character string.
chr() converts a number to a character (code → char). ord() does the reverse—it returns the code point of the first byte of a string (char → code). They are inverse operations for single-byte characters.
For standard ASCII, use 0–127. chr() also accepts 128–255 for extended byte values. In PHP 7.1+, chr() supports Unicode code points beyond 255 when the value maps to a valid character.
A common pattern is a loop from 65 to 90 calling chr($i) to generate letters A through Z. This avoids typing each letter manually and is widely used in star and alphabet pattern tutorials.
In PHP 8.0+, passing a code point outside the allowed range (0 to 1114111, or 0x10FFFF) throws a ValueError. Always validate input when code points come from user data.