The chr function is used to convert ASCII codes into characters.
The ord function is used to convert characters into ASCII codes.
ASCII code is the encoding of characters that can be displayed by a computer. Its value ranges from 0 to 255, including punctuation, letters, numbers, and Chinese characters. During programming, the specified characters are often converted into ASCII codes for comparison.
The following is a function provided by PHP to convert ASCII codes and characters.
1. chr () function
This function is used to convert an ASCII value to a string. The function declaration is as follows:
String chr (int ascii );
2. ord () function
This function is used to convert a string to an ASCII value. The function declaration is as follows:
Int ord (string str );
Example:
Use the chr () and ord () functions to convert strings and ASCII codes. The program code is as follows:
Copy codeThe Code is as follows:
<? Php
$ Str1 = chr (88 );
Echo $ str1; // the return value is X.
$ Str2 = chr (ord (X) + 1 );//
Echo $ str2; // the return value is Y.
Echo "\ t ";
$ Str3 = ord ('s ');
Echo $ str3; // return value: 83
?>
Running result: x y 83