chr()
alinasir853 total contributions
Published Oct 11, 2023
Contribute to Docs
The chr()
function in PHP is used to convert an ASCII value into a single-character string.
Syntax
chr($ascii_value)
It accepts an ASCII value as the argument and returns a string representing the associated character.
The ASCII value can be specified in decimal, octal, or hex values.
- Octal values are defined by a leading
0
. - Hex values are defined by a leading
0x
.
Example
The example below demonstrate the chr()
function when different ASCII values are passed but their equivalent character is the same:
<?php$decimal_ascii = 98; // Decimal format$octal_ascii = 0142; // Octal format (starts with 0)$hexadecimal_ascii = 0x62; // Hexadecimal format (starts with 0x)// Using chr() function to convert ASCII values to characters$char_decimal = chr($decimal_ascii);$char_octal = chr($octal_ascii);$char_hexadecimal = chr($hexadecimal_ascii);// Output the resultsecho "The equivalent character for ASCII 98 in decimal is $char_decimal\n";echo "The equivalent character for ASCII 0142 in octal is $char_octal\n";echo "The equivalent character for ASCII 0x62 in hex is $char_hexadecimal\n";?>
This results in the following output:
The equivalent character for ASCII 98 in decimal is bThe equivalent character for ASCII 0142 in octal is bThe equivalent character for ASCII 0x62 in hex is b
Codebyte Example
The following code is runnable and demonstrates how to use the chr()
function to convert multiple ASCII values into characters.
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.