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.
string chr( $asciiVal)
Parameter: This function accepts a single parameter $asciiVal. This parameter contains a valid ASCII value. The chr() function 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.
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() function when different ASCII are passed but their equivalent character is same.
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() function using arrays.
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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.