PHP | chr() Fucntion
The chr() function is a built-in function in PHP and is used to convert a ASCII value to a character. It accepts an ASCII value as a parameter and returns a string representing a character from the specified ASCII value. 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.
The ASCII values table can be referred from here.
Syntax:
string chr( $asciiVal)
Parameter: This function accepts a single parameter $asciiVal. This parameter contains a valid ASCII value. The chr() fucntion returns the corresponding character of the ASCII value we pass to it as the parameter $asciiVal.
Return Value: The function returns the character whose ASCII value we pass.
Examples:
Input : ASCII=35 ASCII=043 ASCII=0x23 Output : # # # Explanation: The decimal, octal and hex value of '#' is 35, 043 and 0x23 respectively Input : ASCII=48 Output : 0
Below programs illustrate the chr() function in PHP:
Program 1: Program to demonstrate the chr() fucntion when different ASCII are passed but their equivalent character is same.
<?php // PHP program to demonstrate the chr() fucntion $n1 = 35; $n2 = 043; $n3 = 0x23; echo "The equivalent character for ASCII 35 in decimal is "; echo chr($n1), "\n";// Decimal value echo "The equivalent character for ASCII 043 in octal is "; echo chr($n2), "\n"; // Octal value echo "The equivalent character for ASCII 0x23 in hex is "; echo chr($n3); // Hex value ?> |
Output:
The equivalent character for ASCII 35 in decimal is # The equivalent character for ASCII 043 in octal is # The equivalent character for ASCII 0x23 in hex is #
Program 2: Program to demonstrate the chr() fucntion using arrays.
<?php // PHP program to demonstrate the chr() fucntion // in array $a=[48, 49, 50]; foreach($a as $i) { echo "The character equivalent of ASCII value of ", $i, " is "; echo chr($i), "\n"; } ?> |
Output:
The character equivalent of ASCII value of 48 is 0 The character equivalent of ASCII value of 49 is 1 The character equivalent of ASCII value of 50 is 2
Recommended Posts:
- How to remove CSS “top” and “left” attribute with jQuery ?
- How to check if the given date is weekend ?
- How to remove all Non-ASCII characters from the string using JavaScript ?
- How to sort an array of object by two fields in JavaScript ?
- Node.js | Local Module
- How to format current date in MM/DD/YYYY HH:MM:SS format using JavaScript ?
- How to get URL Parameters using JavaScript ?
- Node.js | Utility Module
- ReactJS | Router
- Node.js | Basics
- Node.js | Web Server
- Node.js | Buffer.slice() Method
- PHP | ReflectionExtension isPersistent() Function
- PHP | ReflectionExtension export() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

