chr()
chr() — Generate a single-byte string from a number.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
Returns a one-character string containing the character specified by interpreting codepoint as an unsigned integer.
Signature
chr(int $codepoint): string
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $codepoint | int | An integer between 0 and 255. |
Return value
A single-character string containing the specified byte.
Example
A runnable snippet:
// Assumes the string will be used as ASCII or an ASCII-compatible encoding $str = "The string ends in escape: "; $str .= chr(27); /* add an escape character at the end of $str */ echo $str, PHP_EOL; /* Often this is more useful */ $str = sprintf("The string ends in escape: %c", 27); echo $str, PHP_EOL;
Changelog
| Version | Change |
|---|---|
| 8.5.0 | Passing integers outside the interval [0, 255] is now deprecated. |
| 7.4.0 | The function no longer silently accepts unsupported codepoints, and casts these to 0. |
See also
All PHP 8.5 built-in functions
Reference card built from the PHP manual page chr() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.