Content · Programming · PHP

PHP chr() Function: Usage and Examples

Learn how PHP chr works with practical examples, important behavior, relevant alternatives and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Returns a single byte character for a numeric byte value.

What Is chr() in PHP?

Returns a single byte character for a numeric byte value.

Its syntax is:

chr(int $codepoint): string

Parameters:

  • $codepoint — Accepted as int; required argument.

Return type: string. chr() operates on bytes; for Unicode code points use mb_chr() if available.

Availability: Core PHP.

Example 1: Basic Usage

<?php

echo chr(65);

Output:

A

Chr() operates on bytes; for Unicode code points use mb_chr() if available.

Example 2: A Different Input

<?php

echo bin2hex(chr(10));

Output:

0a

Chr() operates on bytes; for Unicode code points use mb_chr() if available.

Practical Example: Applying It in a Program

<?php

$asciiCodes = [72, 105, 33]; $message = ""; foreach ($asciiCodes as $asciiCode) { $message .= chr($asciiCode); } echo $message;

Output:

Hi!

Chr() operates on bytes; for Unicode code points use mb_chr() if available.

Important Behavior and Edge Cases

  • Key behavior: chr() operates on bytes; for Unicode code points use mb_chr() if available.
  • Environment: Core PHP.

Comparison With Related Functions

FunctionPurpose
chr()Returns a single byte character for a numeric byte value.
ord()ord() reads the byte value of a character; chr() creates one byte from a number.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: chr() operates on bytes; for Unicode code points use mb_chr() if availablechr() operates on bytes; for Unicode code points use mb_chr() if available.

Key Takeaways

  • Returns a single byte character for a numeric byte value.
  • chr() operates on bytes; for Unicode code points use mb_chr() if available.
  • Core PHP.

Summary: chr() operates on bytes; for Unicode code points use mb_chr() if available.

Share this resourceSend this page to someone who may find it useful.