PHP chr() Function

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 5 Examples
String Functions

What You’ll Learn

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.

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.

📝 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.

⚡ Quick Reference

Codechr() ResultMeaning
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

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.

PHP
<?php
$asciiCode = 65;
$character = chr($asciiCode);

echo "ASCII code: $asciiCode\n";
echo "Character:  $character\n";
?>

How It Works

  • ASCII code 65 maps to uppercase letter A.
  • chr() always returns a string, even for a single character.
  • Uppercase A–Z occupy codes 65–90; lowercase a–z occupy 97–122.

Example 2 — Useful ASCII Codes

Convert several common codes to see letters, digits, and symbols side by side.

PHP
<?php
$codes = [65, 90, 97, 122, 48, 57, 33];

foreach ($codes as $code) {
    echo "$code => '" . chr($code) . "'\n";
}
?>

How It Works

Each integer maps to exactly one character. Notice how uppercase and lowercase letters occupy different ranges, and digits 0–9 start at code 48.

📈 Practical Patterns

Patterns you will see in real PHP programs and tutorials.

Example 3 — Round-Trip with ord()

Verify that chr() and ord() are inverse operations for single-byte characters.

PHP
<?php
$letter = "M";
$code   = ord($letter);
$back   = chr($code);

echo "Letter:    $letter\n";
echo "ord():     $code\n";
echo "chr():     $back\n";
echo "Match:     " . ($back === $letter ? "yes\n" : "no\n");
?>

How It Works

ord("M") gives 77, and chr(77) gives "M" back. Note that ord() only reads the first byte of a string, which matters for multi-byte UTF-8 characters.

Example 4 — Generate A–Z with a Loop

The pattern used in alphabet programs: loop from 65 to 90 and print each letter with chr().

PHP
<?php
echo "Uppercase: ";
for ($i = 65; $i <= 90; $i++) {
    echo chr($i);
}
echo "\n";

echo "Lowercase: ";
for ($i = 97; $i <= 122; $i++) {
    echo chr($i);
}
echo "\n";
?>

How It Works

Instead of hard-coding each letter, the loop generates them dynamically. This technique powers many PHP alphabet pattern programs on CodeToFun.

Example 5 — Control Characters (Newline & Tab)

chr() can produce invisible control characters used in formatted output.

PHP
<?php
$newline = chr(10);  // LF (line feed)
$tab     = chr(9);   // horizontal tab

echo "Line 1" . $newline;
echo $tab . "Indented line\n";

echo "ord(newline) = " . ord($newline) . "\n";
echo "ord(tab)     = " . ord($tab) . "\n";
?>

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.

🚀 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.

📝 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.

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.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about chr()

Use these points whenever you convert codes to characters in PHP.

5
Core concepts
🔄 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.

Explore More PHP String Functions

Continue with chunk_split(), count_chars(), and the rest of the string function reference.

Next: chunk_split() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful