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
| Function | Purpose |
|---|---|
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
| Mistake | Correct Approach |
|---|---|
| Ignoring this behavior: chr() operates on bytes; for Unicode code points use mb_chr() if available | chr() 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.