PHP chr() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, chr() returns a string containing the character for a given code. Values are taken modulo 256.

You pass one integer code. It is required. Use ord() to go from character back to code.

Syntax

Call chr with a code. Start with this line.

PHP
$ch = chr(65);
CallWhat it does
chr(65)Returns "A".
chr(10)Returns a newline character.

Codes outside 0–255 wrap with modulo 256.

Library declarations
PHP
chr(int $codepoint): string

Parameters

ParameterDescription
codepointRequired. Integer character code (effectively 0–255).

Return Value

Return ValueDescription
stringA one-character string for that code.

PHP Examples

Example 1 — ASCII Code to Character

Turn 65 into A—the classic first chr() demo.

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

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

$codes = [65, 90, 97, 122, 48, 33];
foreach ($codes as $code) {
    echo "$code => '" . chr($code) . "'\n";
}
?>

How It Works

  1. Pick a code. 65 is uppercase A in ASCII.
  2. Call chr. Returns a one-character string, not an integer.
  3. Map more codes. The loop shows A/Z, a/z, digit 0, and !.
  4. Remember ranges. Upper and lower letters use different code blocks.

Example 2 — Generate A–Z with a Loop

The pattern behind many alphabet programs: loop codes and print chr($i).

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

  1. Upper range. 65 to 90 covers A–Z.
  2. Print each letter. chr($i) converts the current code.
  3. Lower range. 97 to 122 covers a–z the same way.
  4. No hard-coding. You never type every letter by hand.

Example 3 — Round-Trip with ord()

ord() and chr() undo each other 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");

echo "Tab code: " . ord(chr(9)) . "\n";
?>

How It Works

  1. Start with a letter. "M" is the input character.
  2. ord → code. ord("M") returns 77.
  3. chr → letter. chr(77) returns "M" again.
  4. Control chars too. chr(9) is a tab; ord reads it back as 9.

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 alphabet pattern tutorials.

Next: chunk_split()

Learn how to split a string into fixed-size chunks.

chunk_split() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

9 people found this page helpful